koishi-plugin-dify-ai 0.0.0-stage → 0.1.0
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/api/client.d.ts +59 -0
- package/lib/api/types.d.ts +52 -0
- package/lib/commands/app.d.ts +4 -0
- package/lib/commands/misc.d.ts +4 -0
- package/lib/commands/rule.d.ts +4 -0
- package/lib/config.d.ts +83 -0
- package/lib/history.d.ts +41 -0
- package/lib/index.d.ts +8 -0
- package/lib/index.js +1478 -0
- package/lib/locales/en-US.d.ts +9 -0
- package/lib/locales/zh-CN.d.ts +9 -0
- package/lib/requester/files.d.ts +22 -0
- package/lib/requester/render.d.ts +15 -0
- package/lib/requester/runner.d.ts +35 -0
- package/lib/requester/variables.d.ts +5 -0
- package/lib/services/app-service.d.ts +17 -0
- package/lib/state/conversation.d.ts +42 -0
- package/lib/state/tables.d.ts +13 -0
- package/lib/trigger/conditions.d.ts +11 -0
- package/lib/trigger/density.d.ts +15 -0
- package/lib/trigger/digest.d.ts +26 -0
- package/lib/trigger/engine.d.ts +30 -0
- package/lib/trigger/scheduler.d.ts +19 -0
- package/lib/types.d.ts +99 -0
- package/lib/utils/errors.d.ts +2 -0
- package/lib/utils/keys.d.ts +8 -0
- package/lib/utils/session-text.d.ts +22 -0
- package/package.json +38 -4
- package/readme.md +70 -0
- package/README.md +0 -3
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Context } from 'koishi';
|
|
2
|
+
import { ChatBlockingResponse, CompletionBlockingResponse, DifyAppInfo, DifyInputFileObject, DifyUploadFileResult, WorkflowBlockingResponse } from './types';
|
|
3
|
+
/** 构造 DifyClient 所需的最小应用字段 */
|
|
4
|
+
export interface DifyClientApp {
|
|
5
|
+
apiURL: string;
|
|
6
|
+
apiKey: string;
|
|
7
|
+
}
|
|
8
|
+
export declare class DifyClient {
|
|
9
|
+
private http;
|
|
10
|
+
private app;
|
|
11
|
+
private timeout;
|
|
12
|
+
constructor(http: Context['http'], app: DifyClientApp, timeout?: number);
|
|
13
|
+
private buildUrl;
|
|
14
|
+
private buildJsonHeaders;
|
|
15
|
+
private errorFrom;
|
|
16
|
+
/** 非 2xx 时抛出带状态码与 Dify 错误码的 DifyApiError */
|
|
17
|
+
private ensureOk;
|
|
18
|
+
/** blocking JSON 请求(本插件只用 blocking 模式) */
|
|
19
|
+
private request;
|
|
20
|
+
getAppInfo(): Promise<DifyAppInfo>;
|
|
21
|
+
getParameters(): Promise<any>;
|
|
22
|
+
/** chat / agent-chat / advanced-chat(conversation_id 传空串表示新建会话) */
|
|
23
|
+
runChat(body: {
|
|
24
|
+
inputs: Record<string, any>;
|
|
25
|
+
query: string;
|
|
26
|
+
response_mode: 'blocking';
|
|
27
|
+
user: string;
|
|
28
|
+
conversation_id?: string;
|
|
29
|
+
files?: DifyInputFileObject[];
|
|
30
|
+
auto_generate_name?: boolean;
|
|
31
|
+
}): Promise<ChatBlockingResponse>;
|
|
32
|
+
/** workflow 应用 */
|
|
33
|
+
runWorkflow(body: {
|
|
34
|
+
inputs: Record<string, any>;
|
|
35
|
+
response_mode: 'blocking';
|
|
36
|
+
user: string;
|
|
37
|
+
files?: DifyInputFileObject[];
|
|
38
|
+
}): Promise<WorkflowBlockingResponse>;
|
|
39
|
+
/** 文本生成应用 */
|
|
40
|
+
runCompletion(body: {
|
|
41
|
+
inputs: Record<string, any>;
|
|
42
|
+
response_mode: 'blocking';
|
|
43
|
+
user: string;
|
|
44
|
+
files?: DifyInputFileObject[];
|
|
45
|
+
}): Promise<CompletionBlockingResponse>;
|
|
46
|
+
deleteConversation(conversationId: string, user: string): Promise<any>;
|
|
47
|
+
uploadFile(file: {
|
|
48
|
+
data: Buffer | Uint8Array;
|
|
49
|
+
filename: string;
|
|
50
|
+
mime?: string;
|
|
51
|
+
}, user: string): Promise<DifyUploadFileResult>;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 构造客户端的统一入口:凭据缺失时记 warn 并返回 null,
|
|
55
|
+
* 调用方以此直接放弃本次操作。
|
|
56
|
+
*/
|
|
57
|
+
export declare function createDifyClient(ctx: Context, app: DifyClientApp & {
|
|
58
|
+
name?: string;
|
|
59
|
+
}, timeout?: number): DifyClient | null;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** Dify 应用类型(GET /info 返回的 mode 字段) */
|
|
2
|
+
export type DifyAppMode = 'chat' | 'agent-chat' | 'advanced-chat' | 'workflow' | 'completion';
|
|
3
|
+
export interface DifyAppInfo {
|
|
4
|
+
name: string;
|
|
5
|
+
description?: string;
|
|
6
|
+
tags?: string[];
|
|
7
|
+
mode: DifyAppMode;
|
|
8
|
+
author_name?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface DifyInputFileObject {
|
|
11
|
+
type: 'image' | 'document' | 'audio' | 'video' | 'custom';
|
|
12
|
+
transfer_method: 'remote_url' | 'local_file';
|
|
13
|
+
url?: string;
|
|
14
|
+
upload_file_id?: string;
|
|
15
|
+
}
|
|
16
|
+
/** workflow outputs 里的文件对象形态 */
|
|
17
|
+
export interface DifyFilePayload {
|
|
18
|
+
url: string;
|
|
19
|
+
type?: string;
|
|
20
|
+
}
|
|
21
|
+
/** POST /chat-messages(blocking):完整回复 + 会话 id */
|
|
22
|
+
export interface ChatBlockingResponse {
|
|
23
|
+
answer: string;
|
|
24
|
+
conversation_id?: string;
|
|
25
|
+
}
|
|
26
|
+
/** POST /workflows/run(blocking):结果都在 data 内 */
|
|
27
|
+
export interface WorkflowBlockingResponse {
|
|
28
|
+
data: {
|
|
29
|
+
id?: string;
|
|
30
|
+
/** succeeded / failed / stopped / partial-succeeded / paused */
|
|
31
|
+
status?: string;
|
|
32
|
+
outputs?: Record<string, any> | null;
|
|
33
|
+
error?: string | null;
|
|
34
|
+
total_tokens?: number;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** POST /completion-messages(blocking):文本生成应用无会话概念 */
|
|
38
|
+
export interface CompletionBlockingResponse {
|
|
39
|
+
answer: string;
|
|
40
|
+
}
|
|
41
|
+
export interface DifyUploadFileResult {
|
|
42
|
+
id: string;
|
|
43
|
+
name?: string;
|
|
44
|
+
mime_type?: string;
|
|
45
|
+
size?: number;
|
|
46
|
+
[key: string]: any;
|
|
47
|
+
}
|
|
48
|
+
export declare class DifyApiError extends Error {
|
|
49
|
+
status: number;
|
|
50
|
+
code: string;
|
|
51
|
+
constructor(message: string, status?: number, code?: string);
|
|
52
|
+
}
|
package/lib/config.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Schema } from 'koishi';
|
|
2
|
+
import { DifyRule } from './types';
|
|
3
|
+
export type AppTypeSetting = 'auto' | 'chat' | 'agent-chat' | 'advanced-chat' | 'workflow' | 'completion';
|
|
4
|
+
export interface AppConfigEntry {
|
|
5
|
+
name: string;
|
|
6
|
+
/** 会话绑定用的稳定标识;留空回退应用名 */
|
|
7
|
+
appId: string;
|
|
8
|
+
apiURL: string;
|
|
9
|
+
apiKey: string;
|
|
10
|
+
appType: AppTypeSetting;
|
|
11
|
+
}
|
|
12
|
+
/** 条件的 union-tagged 形态:选择分支后仅出现该分支的字段 */
|
|
13
|
+
export type ConditionEntry = {
|
|
14
|
+
type: 'keyword-prefix';
|
|
15
|
+
keywords: string;
|
|
16
|
+
} | {
|
|
17
|
+
type: 'keyword-contains';
|
|
18
|
+
keywords: string;
|
|
19
|
+
} | {
|
|
20
|
+
type: 'at-bot';
|
|
21
|
+
} | {
|
|
22
|
+
type: 'at-user';
|
|
23
|
+
userIds: string;
|
|
24
|
+
} | {
|
|
25
|
+
type: 'quote-bot';
|
|
26
|
+
} | {
|
|
27
|
+
type: 'quote-user';
|
|
28
|
+
userIds: string;
|
|
29
|
+
} | {
|
|
30
|
+
type: 'schedule';
|
|
31
|
+
cron?: string;
|
|
32
|
+
intervalMinutes?: number;
|
|
33
|
+
} | {
|
|
34
|
+
type: 'density';
|
|
35
|
+
densityCount?: number;
|
|
36
|
+
densityWindowSeconds?: number;
|
|
37
|
+
};
|
|
38
|
+
/** 变量映射的条件分支形态:先选来源分支,再填该分支的字段(历史消息是会话字段下的一项) */
|
|
39
|
+
export type VariableEntry = {
|
|
40
|
+
source: 'session';
|
|
41
|
+
field: string;
|
|
42
|
+
key: string;
|
|
43
|
+
limit?: number;
|
|
44
|
+
} | {
|
|
45
|
+
source: 'meta';
|
|
46
|
+
field: string;
|
|
47
|
+
key: string;
|
|
48
|
+
};
|
|
49
|
+
/** 作用域的一行:维度 + 值 */
|
|
50
|
+
export interface ScopeRow {
|
|
51
|
+
dimension: string;
|
|
52
|
+
value: string;
|
|
53
|
+
}
|
|
54
|
+
export interface RuleEntry {
|
|
55
|
+
name: string;
|
|
56
|
+
enabled: boolean;
|
|
57
|
+
priority: number;
|
|
58
|
+
app: string;
|
|
59
|
+
conversationScope: string;
|
|
60
|
+
conditions: ConditionEntry[];
|
|
61
|
+
scope: ScopeRow[];
|
|
62
|
+
variables: VariableEntry[];
|
|
63
|
+
}
|
|
64
|
+
export interface Config {
|
|
65
|
+
apps: AppConfigEntry[];
|
|
66
|
+
rules: RuleEntry[];
|
|
67
|
+
parseElements: boolean;
|
|
68
|
+
quoteTrigger: boolean;
|
|
69
|
+
maxSegmentLength: number;
|
|
70
|
+
uploadFiles: boolean;
|
|
71
|
+
requestTimeout: number;
|
|
72
|
+
historyLimit: number;
|
|
73
|
+
sendErrors: boolean;
|
|
74
|
+
}
|
|
75
|
+
export declare const Config: Schema<Config>;
|
|
76
|
+
/**
|
|
77
|
+
* 配置规范化:应用表 + 按优先级降序的规则列表。
|
|
78
|
+
* 坏条件/无名变量直接丢弃;引用不存在应用的规则保留但运行时会 warn 跳过。
|
|
79
|
+
*/
|
|
80
|
+
export declare function normalizeConfig(config: Config): {
|
|
81
|
+
apps: Map<string, AppConfigEntry>;
|
|
82
|
+
rules: DifyRule[];
|
|
83
|
+
};
|
package/lib/history.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Session } from 'koishi';
|
|
2
|
+
/** 被引用消息的呈现所需字段(无消息 ID,与格式规范一致) */
|
|
3
|
+
export interface QuotedMessage {
|
|
4
|
+
timestamp: number;
|
|
5
|
+
userId: string;
|
|
6
|
+
username: string;
|
|
7
|
+
content: string;
|
|
8
|
+
}
|
|
9
|
+
export interface HistoryEntry {
|
|
10
|
+
role: 'user' | 'assistant';
|
|
11
|
+
userId: string;
|
|
12
|
+
username: string;
|
|
13
|
+
messageId?: string;
|
|
14
|
+
/** 纯文本 + 图片占位符 */
|
|
15
|
+
content: string;
|
|
16
|
+
timestamp: number;
|
|
17
|
+
quote?: QuotedMessage;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 统一消息格式(历史消息变量 / 定时摘要 / 密度触发共用):
|
|
21
|
+
* `[时间] 昵称 (用户ID) [msg:消息ID](引用了 [时间] 昵称 (用户ID) 说:内容)说:内容`
|
|
22
|
+
* 引用段与外层同构但不展示消息 ID;无引用则整段省略。
|
|
23
|
+
*/
|
|
24
|
+
export declare function formatHistoryEntry(entry: HistoryEntry): string;
|
|
25
|
+
/** 多条消息逐行拼接 */
|
|
26
|
+
export declare function formatHistoryEntries(entries: HistoryEntry[]): string;
|
|
27
|
+
/** 从会话消息构造记录条目(历史缓冲 / 密度窗口 / 定时窗口共用) */
|
|
28
|
+
export declare function buildHistoryEntry(session: Session, role?: 'user' | 'assistant'): HistoryEntry | null;
|
|
29
|
+
/**
|
|
30
|
+
* 每个频道在内存中保留最近消息(含 bot 自己的回复),
|
|
31
|
+
* 供「历史消息注入」变量与密度触发使用。重启后清空,属可接受损耗。
|
|
32
|
+
*/
|
|
33
|
+
export declare class MessageHistory {
|
|
34
|
+
limit: number;
|
|
35
|
+
private buffers;
|
|
36
|
+
constructor(limit?: number);
|
|
37
|
+
recordEntry(channelKey: string, entry: HistoryEntry): void;
|
|
38
|
+
get(channelKey: string, limit?: number): HistoryEntry[];
|
|
39
|
+
/** 序列化为统一消息格式的逐行拼接(变量值即完整聊天记录文本) */
|
|
40
|
+
toVariable(entries: HistoryEntry[]): string;
|
|
41
|
+
}
|
package/lib/index.d.ts
ADDED