koishi-plugin-dify-ai 0.1.13 → 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 +1 -2
- package/lib/index.d.ts +2 -0
- package/lib/index.js +50 -17
- package/lib/requester/runner.d.ts +2 -0
- package/lib/state/conversation.d.ts +0 -2
- package/lib/state/tables.d.ts +7 -0
- 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;
|
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
|
|
|
@@ -200,8 +201,7 @@ var Config = import_koishi.Schema.intersect([
|
|
|
200
201
|
import_koishi.Schema.object({
|
|
201
202
|
apps: import_koishi.Schema.array(
|
|
202
203
|
import_koishi.Schema.object({
|
|
203
|
-
name: import_koishi.Schema.string().required().description("
|
|
204
|
-
appId: import_koishi.Schema.string().default("").description("应用 ID,会话绑定用,留空取应用名"),
|
|
204
|
+
name: import_koishi.Schema.string().required().description("应用 ID"),
|
|
205
205
|
apiURL: import_koishi.Schema.string().required().role("link").description("API 地址"),
|
|
206
206
|
apiKey: import_koishi.Schema.string().required().role("secret").description("App Key"),
|
|
207
207
|
appType: appTypeSchema.default("auto").description("应用类型")
|
|
@@ -214,7 +214,7 @@ var Config = import_koishi.Schema.intersect([
|
|
|
214
214
|
name: import_koishi.Schema.string().required().description("规则名"),
|
|
215
215
|
enabled: import_koishi.Schema.boolean().default(true).description("启用"),
|
|
216
216
|
priority: import_koishi.Schema.number().default(-1).description("优先级,大者先匹配"),
|
|
217
|
-
app: import_koishi.Schema.string().required().description("
|
|
217
|
+
app: import_koishi.Schema.string().required().description("应用 ID"),
|
|
218
218
|
conversationScope: import_koishi.Schema.union([
|
|
219
219
|
import_koishi.Schema.const("per-guild").description("按群聊"),
|
|
220
220
|
import_koishi.Schema.const("per-user").description("按用户"),
|
|
@@ -1795,6 +1795,8 @@ var RequestRunner = class {
|
|
|
1795
1795
|
static {
|
|
1796
1796
|
__name(this, "RequestRunner");
|
|
1797
1797
|
}
|
|
1798
|
+
/** 启动期类型探测任务:auto 应用首次请求在探测完成前等待它(settle 即放行,失败仍回退 chat) */
|
|
1799
|
+
probeTask;
|
|
1798
1800
|
/** 应用类型:schema 显式指定优先,auto 时用探测结果(未探测到回退 chat) */
|
|
1799
1801
|
appTypeOf(app) {
|
|
1800
1802
|
return app.appType !== "auto" ? app.appType : this.appTypes.get(app.name) ?? FALLBACK_APP_TYPE;
|
|
@@ -1810,7 +1812,7 @@ var RequestRunner = class {
|
|
|
1810
1812
|
const client = createDifyClient(this.ctx, app, this.config.requestTimeout);
|
|
1811
1813
|
if (!client) return;
|
|
1812
1814
|
const requestCtx = {
|
|
1813
|
-
bindingKey: this.conversationStore.computeBindingKey(app.
|
|
1815
|
+
bindingKey: this.conversationStore.computeBindingKey(app.name, rule, fireCtx),
|
|
1814
1816
|
difyUser: this.conversationStore.computeDifyUser(rule, fireCtx),
|
|
1815
1817
|
inputs: resolveVariables(rule, fireCtx, this.history),
|
|
1816
1818
|
uploadedFiles: []
|
|
@@ -1871,6 +1873,10 @@ var RequestRunner = class {
|
|
|
1871
1873
|
}
|
|
1872
1874
|
/** 按应用类型分流:workflow/completion 无会话,chat 系续接 conversation_id */
|
|
1873
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
|
+
}
|
|
1874
1880
|
const base = {
|
|
1875
1881
|
inputs: requestCtx.inputs,
|
|
1876
1882
|
response_mode: "blocking",
|
|
@@ -2045,8 +2051,7 @@ var ConversationStore = class {
|
|
|
2045
2051
|
for (const row of matched) {
|
|
2046
2052
|
if (row.difyConversationId) {
|
|
2047
2053
|
const { appId } = this.parseBindingKey(row.bindingKey);
|
|
2048
|
-
const
|
|
2049
|
-
const client = app && createDifyClient(this.ctx, app);
|
|
2054
|
+
const client = appId && this.apps.has(appId) ? createDifyClient(this.ctx, this.apps.get(appId)) : void 0;
|
|
2050
2055
|
if (client) {
|
|
2051
2056
|
await client.deleteConversation(row.difyConversationId, this.difyUserOfBindingKey(row.bindingKey)).catch((error) => this.ctx.logger.warn("删除 Dify 会话失败:%s", error));
|
|
2052
2057
|
}
|
|
@@ -2056,13 +2061,6 @@ var ConversationStore = class {
|
|
|
2056
2061
|
}
|
|
2057
2062
|
return reset;
|
|
2058
2063
|
}
|
|
2059
|
-
/** 按应用 ID(apps 配置的 appId 字段)反查应用 */
|
|
2060
|
-
findByAppId(appId) {
|
|
2061
|
-
for (const app of this.apps.values()) {
|
|
2062
|
-
if (app.appId && app.appId === appId) return app;
|
|
2063
|
-
}
|
|
2064
|
-
return void 0;
|
|
2065
|
-
}
|
|
2066
2064
|
};
|
|
2067
2065
|
|
|
2068
2066
|
// src/state/tables.ts
|
|
@@ -2073,8 +2071,17 @@ function defineConversationTable(ctx) {
|
|
|
2073
2071
|
}, { primary: "bindingKey" });
|
|
2074
2072
|
}
|
|
2075
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");
|
|
2076
2082
|
function defineTables(ctx) {
|
|
2077
2083
|
defineConversationTable(ctx);
|
|
2084
|
+
defineProbeTable(ctx);
|
|
2078
2085
|
}
|
|
2079
2086
|
__name(defineTables, "defineTables");
|
|
2080
2087
|
|
|
@@ -2096,6 +2103,25 @@ __name(registerMiscCommands, "registerMiscCommands");
|
|
|
2096
2103
|
// src/index.ts
|
|
2097
2104
|
var name = "dify-ai";
|
|
2098
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
|
+
`;
|
|
2099
2125
|
var PROBE_TIMEOUT_MS = 1e4;
|
|
2100
2126
|
function apply(ctx, config) {
|
|
2101
2127
|
const startedAt = Date.now();
|
|
@@ -2109,11 +2135,16 @@ function apply(ctx, config) {
|
|
|
2109
2135
|
const conversationStore = new ConversationStore(ctx, apps);
|
|
2110
2136
|
const runner = new RequestRunner(ctx, config, conversationStore, history, apps, appTypes);
|
|
2111
2137
|
const engine = new TriggerEngine(ctx, runner, history, rules, cooldown);
|
|
2112
|
-
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
|
+
}
|
|
2113
2143
|
engine.start();
|
|
2114
2144
|
ctx.logger.info("插件装配完成,耗时 %dms", Date.now() - startedAt);
|
|
2115
2145
|
const probeStart = Date.now();
|
|
2116
|
-
|
|
2146
|
+
runner.probeTask = probeAppTypes(ctx, apps, appTypes);
|
|
2147
|
+
void runner.probeTask.then(() => {
|
|
2117
2148
|
ctx.logger.info("应用类型探测完成,耗时 %dms", Date.now() - probeStart);
|
|
2118
2149
|
});
|
|
2119
2150
|
});
|
|
@@ -2131,6 +2162,7 @@ async function probeAppTypes(ctx, apps, appTypes) {
|
|
|
2131
2162
|
try {
|
|
2132
2163
|
const info = await client.getAppInfo();
|
|
2133
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));
|
|
2134
2166
|
ctx.logger.info("应用 %s 类型探测:%s", app.name, info.mode);
|
|
2135
2167
|
} catch (error) {
|
|
2136
2168
|
ctx.logger.warn("应用 %s 类型探测失败(按 chat 处理):%s", app.name, error);
|
|
@@ -2144,5 +2176,6 @@ __name(probeAppTypes, "probeAppTypes");
|
|
|
2144
2176
|
Config,
|
|
2145
2177
|
apply,
|
|
2146
2178
|
inject,
|
|
2147
|
-
name
|
|
2179
|
+
name,
|
|
2180
|
+
usage
|
|
2148
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>;
|
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/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",
|