@tais00/api 0.7.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.
@@ -0,0 +1,604 @@
1
+ import { IDifyAppItem, IDifyAppSiteSetting } from '@dify-chat/core';
2
+ import { IAgentThought, IRetrieverResource } from './../types';
3
+ import { IFileType } from './../types/file';
4
+ /**
5
+ * 用户输入表单控件类型
6
+ */
7
+ export type IUserInputFormItemType = 'text-input' | 'paragraph' | 'select' | 'number' | 'file' | 'file-list';
8
+ /**
9
+ * 用户输入表单控件对象
10
+ */
11
+ export interface IUserInputFormItemValueBase {
12
+ default: string;
13
+ label: string;
14
+ required: boolean;
15
+ hide?: boolean;
16
+ variable: string;
17
+ options?: string[];
18
+ /**
19
+ * 最大长度
20
+ */
21
+ max_length?: number;
22
+ type: IUserInputFormItemType;
23
+ allowed_file_types?: IFileType[];
24
+ }
25
+ /**
26
+ * 应用输入配置
27
+ */
28
+ export type IUserInputForm = Record<IUserInputFormItemType, IUserInputFormItemValueBase>;
29
+ /**
30
+ * 获取应用参数-响应体
31
+ */
32
+ export interface IGetAppParametersResponse {
33
+ /**
34
+ * 开场白
35
+ */
36
+ opening_statement?: string;
37
+ /**
38
+ * 用户输入表单
39
+ */
40
+ user_input_form: IUserInputForm[];
41
+ /**
42
+ * 开场建议问题
43
+ */
44
+ suggested_questions?: string[];
45
+ /**
46
+ * 下一轮问题建议
47
+ */
48
+ suggested_questions_after_answer: {
49
+ /**
50
+ * 是否启用
51
+ */
52
+ enabled: boolean;
53
+ };
54
+ /**
55
+ * 文件上传参数
56
+ */
57
+ file_upload: {
58
+ /**
59
+ * 是否允许上传文件
60
+ */
61
+ enabled: boolean;
62
+ /**
63
+ * 允许上传的文件后缀名
64
+ */
65
+ allowed_file_extensions: string[];
66
+ /**
67
+ * 允许上传的文件类型
68
+ */
69
+ allowed_file_types: IFileType[];
70
+ /**
71
+ * 允许的上传方式 - remote_url-远程地址 local_file-本地文件
72
+ */
73
+ allowed_file_upload_methods: Array<'remote_url' | 'local_file'>;
74
+ /**
75
+ * 文件上传配置
76
+ */
77
+ fileUploadConfig: {
78
+ /**
79
+ * 文件大小限制(单位:MB)
80
+ */
81
+ file_size_limit: number;
82
+ /**
83
+ * 批量上传文件数量限制
84
+ */
85
+ batch_count_limit: number;
86
+ /**
87
+ * 图片文件大小限制(单位:MB)
88
+ */
89
+ image_file_size_limit: number;
90
+ /**
91
+ * 视频文件大小限制(单位:MB)
92
+ */
93
+ video_file_size_limit: number;
94
+ /**
95
+ * 音频文件大小限制(单位:MB)
96
+ */
97
+ audio_file_size_limit: number;
98
+ /**
99
+ * 工作流文件上传限制(单位:MB)
100
+ */
101
+ workflow_file_upload_limit: number;
102
+ };
103
+ /**
104
+ * 图片上传配置
105
+ */
106
+ image: {
107
+ /**
108
+ * 是否启用图片上传
109
+ */
110
+ enabled: false;
111
+ /**
112
+ * 允许上传的图片最大数量
113
+ */
114
+ number_limits: 3;
115
+ /**
116
+ * 允许的上传方式 - remote_url-远程地址 local_file-本地文件
117
+ */
118
+ transfer_methods: ['local_file', 'remote_url'];
119
+ };
120
+ /**
121
+ * 允许上传的文件数量
122
+ */
123
+ number_limits: number;
124
+ };
125
+ /**
126
+ * 文本转语音配置
127
+ */
128
+ text_to_speech: {
129
+ /**
130
+ * 是否启用
131
+ */
132
+ enabled: boolean;
133
+ /**
134
+ * 是否开启自动播放 enabled-启用 disabled-禁用
135
+ */
136
+ autoPlay: 'enabled' | 'disabled';
137
+ /**
138
+ * 语言
139
+ */
140
+ language: string;
141
+ /**
142
+ * 音色
143
+ */
144
+ voice: string;
145
+ };
146
+ /**
147
+ * 语音转文本配置
148
+ */
149
+ speech_to_text: {
150
+ /**
151
+ * 是否启用
152
+ */
153
+ enabled: boolean;
154
+ };
155
+ }
156
+ export interface IConversationItem {
157
+ created_at: number;
158
+ id: string;
159
+ inputs: Record<string, unknown>;
160
+ introduction: string;
161
+ name: string;
162
+ status: 'normal';
163
+ updated_at: number;
164
+ }
165
+ /**
166
+ * 获取工作流执行结果-响应体
167
+ */
168
+ export interface IGetWorkflowResultResponse {
169
+ /** workflow 执行 ID */
170
+ id: string;
171
+ /** 关联的 Workflow ID */
172
+ workflow_id: string;
173
+ /** 执行状态 running / succeeded / failed / stopped */
174
+ status: string;
175
+ /** 任务输入内容 */
176
+ inputs: object;
177
+ /** 任务输出内容 */
178
+ outputs: object;
179
+ /** 错误原因 */
180
+ error: string;
181
+ /** 任务执行总步数 */
182
+ total_steps: number;
183
+ /** 任务执行总 tokens */
184
+ total_tokens: number;
185
+ /** 任务开始时间 */
186
+ created_at: number;
187
+ /** 任务结束时间 */
188
+ finished_at: number;
189
+ /** 耗时(s) */
190
+ elapsed_time: number;
191
+ }
192
+ /**
193
+ * 获取会话列表-参数
194
+ */
195
+ interface IListConversationsRequest {
196
+ /**
197
+ * 返回条数
198
+ */
199
+ limit: number;
200
+ }
201
+ /**
202
+ * 获取会话列表-响应体
203
+ */
204
+ interface IGetConversationListResponse {
205
+ data: IConversationItem[];
206
+ }
207
+ /**
208
+ * 消息文件所属类型
209
+ */
210
+ export declare enum MessageFileBelongsToEnum {
211
+ 'user' = "user",// 用户
212
+ 'assistant' = "assistant"
213
+ }
214
+ /**
215
+ * 消息文件 Item 结构
216
+ */
217
+ interface IMessageFileItem {
218
+ id: string;
219
+ filename: string;
220
+ type: string;
221
+ url: string;
222
+ mime_type: string;
223
+ size: number;
224
+ transfer_method: string;
225
+ belongs_to: MessageFileBelongsToEnum;
226
+ upload_file_id: string;
227
+ }
228
+ /**
229
+ * 会话历史 Item 结构
230
+ */
231
+ interface IMessageItem {
232
+ /**
233
+ * 消息 ID
234
+ */
235
+ id: string;
236
+ /**
237
+ * 对话 ID
238
+ */
239
+ conversation_id: string;
240
+ /**
241
+ * 输入参数,键值对形式
242
+ */
243
+ inputs: Record<string, string>;
244
+ /**
245
+ * 问题
246
+ */
247
+ query: string;
248
+ /**
249
+ * 答案
250
+ */
251
+ answer: string;
252
+ /**
253
+ * 消息关联的文件列表
254
+ */
255
+ message_files?: IMessageFileItem[];
256
+ /**
257
+ * 消息反馈信息
258
+ */
259
+ feedback?: {
260
+ /**
261
+ * 反馈类型,like 表示点赞,dislike 表示点踩
262
+ */
263
+ rating: 'like' | 'dislike';
264
+ };
265
+ /**
266
+ * 消息状态,normal 表示正常,error 表示出错
267
+ */
268
+ status: 'normal' | 'error';
269
+ /**
270
+ * 错误信息,若消息状态为 error 时,该字段包含具体错误信息,否则为 null
271
+ */
272
+ error: string | null;
273
+ /**
274
+ * 智能体思考过程列表
275
+ */
276
+ agent_thoughts?: IAgentThought[];
277
+ /**
278
+ * 消息创建时间戳
279
+ */
280
+ created_at: number;
281
+ /**
282
+ * 知识库引用列表
283
+ */
284
+ retriever_resources?: IRetrieverResource[];
285
+ }
286
+ interface IListMessagesResponse {
287
+ data: IMessageItem[];
288
+ has_more: boolean;
289
+ limit: number;
290
+ }
291
+ export interface IDifyApiOptions {
292
+ /**
293
+ * 用户
294
+ */
295
+ user: string;
296
+ /**
297
+ * API 前缀,默认 https://api.dify.ai/v1
298
+ */
299
+ apiBase: string;
300
+ /**
301
+ * Dify APP API 密钥
302
+ */
303
+ apiKey: string;
304
+ }
305
+ export type IGetAppInfoResponse = IDifyAppItem['info'];
306
+ export interface IGetAppMetaResponse {
307
+ tool_icons: {
308
+ dalle2: string;
309
+ api_tool: {
310
+ background: string;
311
+ content: string;
312
+ };
313
+ };
314
+ }
315
+ export interface IFileBase {
316
+ /**
317
+ * 文件类型
318
+ */
319
+ type: IFileType;
320
+ }
321
+ export interface IFileRemote extends IFileBase {
322
+ /**
323
+ * 传递方式 remote_url-远程地址 local_file-本地文件
324
+ */
325
+ transfer_method: 'remote_url';
326
+ /**
327
+ * 图片地址(仅当传递方式为 remote_url 时)
328
+ */
329
+ url?: string;
330
+ }
331
+ export interface IFileLocal extends IFileBase {
332
+ /**
333
+ * 传递方式 remote_url-远程地址 local_file-本地文件
334
+ */
335
+ transfer_method: 'local_file';
336
+ /**
337
+ * 上传文件 ID(仅当传递方式为 local_file 时)
338
+ */
339
+ upload_file_id?: string;
340
+ }
341
+ export type IFile = IFileRemote | IFileLocal;
342
+ /**
343
+ * 上传文件接口详情
344
+ */
345
+ export interface IUploadFileResponse {
346
+ id: string;
347
+ name: string;
348
+ size: number;
349
+ extension: string;
350
+ mime_type: string;
351
+ created_by: number;
352
+ created_at: number;
353
+ }
354
+ /**
355
+ * 语音转文字的响应
356
+ */
357
+ export interface IAudio2TextResponse {
358
+ /**
359
+ * 生成的文本内容
360
+ */
361
+ text: string;
362
+ }
363
+ /**
364
+ * 创建标注请求参数
365
+ */
366
+ export interface ICreateAnnotationRequest {
367
+ /**
368
+ * 问题
369
+ */
370
+ question: string;
371
+ /**
372
+ * 答案内容
373
+ */
374
+ answer: string;
375
+ }
376
+ /**
377
+ * 标注响应
378
+ */
379
+ export interface IAnnotationItem {
380
+ id: string;
381
+ question: string;
382
+ answer: string;
383
+ hit_count: number;
384
+ created_at: number;
385
+ }
386
+ /**
387
+ * 获取标注列表请求参数
388
+ */
389
+ export interface IGetAnnotationListRequest {
390
+ page?: number;
391
+ limit?: number;
392
+ }
393
+ /**
394
+ * 获取标注列表响应
395
+ */
396
+ export interface IGetAnnotationListResponse {
397
+ data: IAnnotationItem[];
398
+ has_more: boolean;
399
+ limit: number;
400
+ total: number;
401
+ page: number;
402
+ }
403
+ /**
404
+ * 更新标注请求参数
405
+ */
406
+ export interface IUpdateAnnotationRequest {
407
+ question: string;
408
+ answer: string;
409
+ }
410
+ /**
411
+ * Dify API 类
412
+ */
413
+ export declare class DifyApi {
414
+ constructor(options: IDifyApiOptions);
415
+ options: IDifyApiOptions;
416
+ private baseRequest;
417
+ /**
418
+ * 更新 API 配置, 一般在切换应用时调用
419
+ */
420
+ updateOptions: (options: IDifyApiOptions) => void;
421
+ /**
422
+ * 获取应用基本信息
423
+ */
424
+ getAppInfo: () => Promise<{
425
+ name: string;
426
+ mode?: import("@dify-chat/core").AppModeEnums;
427
+ description: string;
428
+ tags: string[];
429
+ }>;
430
+ /**
431
+ * 获取应用 Meta 信息
432
+ */
433
+ getAppMeta: () => Promise<IGetAppMetaResponse>;
434
+ /**
435
+ * 获取应用参数
436
+ */
437
+ getAppParameters: () => Promise<IGetAppParametersResponse>;
438
+ /**
439
+ * 获取应用 WebAPP 设置
440
+ * @Limited Dify v1.4.0 版本开始支持
441
+ */
442
+ getAppSiteSetting: () => Promise<IDifyAppSiteSetting>;
443
+ /**
444
+ * 获取当前用户的会话列表(默认返回最近20条)
445
+ */
446
+ listConversations: (params?: IListConversationsRequest) => Promise<IGetConversationListResponse>;
447
+ /**
448
+ * 会话重命名
449
+ */
450
+ renameConversation: (params: {
451
+ /**
452
+ * 会话 ID
453
+ */
454
+ conversation_id: string;
455
+ /**
456
+ * 名称,若 auto_generate 为 true 时,该参数可不传。
457
+ */
458
+ name?: string;
459
+ /**
460
+ * 自动生成标题,默认 false
461
+ */
462
+ auto_generate?: boolean;
463
+ }) => Promise<unknown>;
464
+ /**
465
+ * 删除会话
466
+ */
467
+ deleteConversation: (conversation_id: string) => Promise<Response>;
468
+ /**
469
+ * 获取会话历史消息
470
+ */
471
+ listMessages: (conversation_id: string) => Promise<IListMessagesResponse>;
472
+ /**
473
+ * 发送对话消息
474
+ */
475
+ sendMessage: (params: {
476
+ /**
477
+ * 对话 ID
478
+ */
479
+ conversation_id?: string;
480
+ /**
481
+ * 输入参数
482
+ */
483
+ inputs: Record<string, string>;
484
+ /**
485
+ * 附件
486
+ */
487
+ files: IFile[];
488
+ /**
489
+ * 用户
490
+ */
491
+ user: string;
492
+ /**
493
+ * 响应模式
494
+ */
495
+ response_mode: "streaming";
496
+ /**
497
+ * 问题
498
+ */
499
+ query: string;
500
+ }) => Promise<Response>;
501
+ /**
502
+ * 停止对话流式响应
503
+ */
504
+ stopTask: (taskId: string) => Promise<unknown>;
505
+ /**
506
+ * 上传文件
507
+ */
508
+ uploadFile: (file: File) => Promise<IUploadFileResponse>;
509
+ /**
510
+ * 获取下一轮建议问题列表
511
+ */
512
+ getNextSuggestions: (params: {
513
+ /**
514
+ * 消息 ID
515
+ */
516
+ message_id: string;
517
+ }) => Promise<{
518
+ data: string[];
519
+ }>;
520
+ /**
521
+ * 消息反馈
522
+ */
523
+ createMessageFeedback: (params: {
524
+ /**
525
+ * 消息 ID
526
+ */
527
+ messageId: string;
528
+ /**
529
+ * 反馈类型 like-点赞 dislike-点踩 null-取消
530
+ */
531
+ rating: "like" | "dislike" | null;
532
+ /**
533
+ * 反馈内容
534
+ */
535
+ content: string;
536
+ }) => Promise<{
537
+ result: "success";
538
+ }>;
539
+ /**
540
+ * 文字转语音
541
+ */
542
+ text2Audio: (params: {
543
+ /**
544
+ * 消息 ID,优先级高于 text
545
+ */
546
+ message_id: string;
547
+ } | {
548
+ /**
549
+ * 文本内容
550
+ */
551
+ text: string;
552
+ }) => Promise<Response>;
553
+ /**
554
+ * 语音转文本
555
+ * @param file 语音文件。 支持格式:['mp3', 'mp4', 'mpeg', 'mpga', 'm4a', 'wav', 'webm'] 文件大小限制:15MB
556
+ */
557
+ audio2Text: (file: File) => Promise<IAudio2TextResponse>;
558
+ /**
559
+ * 执行 workflow
560
+ */
561
+ runWorkflow: (params: {
562
+ inputs: Record<string, IFile[] | unknown>;
563
+ }) => Promise<Response>;
564
+ /**
565
+ * 获取 workflow 执行情况
566
+ */
567
+ getWorkflowResult: (params: {
568
+ workflow_run_id: string;
569
+ }) => Promise<IGetWorkflowResultResponse>;
570
+ /**
571
+ * 执行文本生成
572
+ */
573
+ completion: (params: {
574
+ inputs: Record<string, IFile[] | unknown>;
575
+ }) => Promise<Response>;
576
+ /**
577
+ * 创建标注
578
+ */
579
+ createAnnotation: (params: ICreateAnnotationRequest) => Promise<IAnnotationItem>;
580
+ /**
581
+ * 获取标注列表
582
+ */
583
+ getAnnotationList: (params?: IGetAnnotationListRequest) => Promise<IGetAnnotationListResponse>;
584
+ /**
585
+ * 更新标注
586
+ */
587
+ updateAnnotation: (annotationId: string, params: IUpdateAnnotationRequest) => Promise<IAnnotationItem>;
588
+ /**
589
+ * 删除标注
590
+ */
591
+ deleteAnnotation: (annotationId: string) => Promise<Response>;
592
+ /**
593
+ * 获取文件预览
594
+ */
595
+ filePreview: (params: {
596
+ file_id: string;
597
+ as_attachment?: boolean;
598
+ }) => Promise<Response>;
599
+ }
600
+ /**
601
+ * 创建 Dify API 实例
602
+ */
603
+ export declare const createDifyApiInstance: (options: IDifyApiOptions) => DifyApi;
604
+ export {};
@@ -0,0 +1,22 @@
1
+ /**
2
+ * 未授权错误类
3
+ */
4
+ export declare class UnauthorizedError extends Error {
5
+ constructor(message: string);
6
+ }
7
+ export declare class XRequest {
8
+ constructor(options: {
9
+ baseURL: string;
10
+ apiKey: string;
11
+ });
12
+ options: {
13
+ baseURL: string;
14
+ apiKey: string;
15
+ };
16
+ baseRequest(url: string, options: RequestInit): Promise<Response>;
17
+ jsonRequest(url: string, options: RequestInit): Promise<unknown>;
18
+ get(url: string, params?: Record<string, string>, headers?: Record<string, string>): Promise<unknown>;
19
+ post(url: string, params?: Record<string, unknown>, headers?: Record<string, string>): Promise<unknown>;
20
+ delete(url: string, params?: Record<string, unknown>, headers?: Record<string, string>): Promise<Response>;
21
+ }
22
+ export default XRequest;
@@ -0,0 +1,61 @@
1
+ /**
2
+ * 对话接口流式输出事件类型列表
3
+ */
4
+ export declare enum EventEnum {
5
+ /**
6
+ * 消息事件,代表普通消息的发送或接收
7
+ */
8
+ MESSAGE = "message",
9
+ /**
10
+ * 代理消息事件,代表代理发送的消息
11
+ */
12
+ AGENT_MESSAGE = "agent_message",
13
+ /**
14
+ * 代理思考事件,代表代理在处理过程中的思考信息
15
+ */
16
+ AGENT_THOUGHT = "agent_thought",
17
+ /**
18
+ * 消息文件事件,代表与消息相关的文件信息
19
+ */
20
+ MESSAGE_FILE = "message_file",
21
+ /**
22
+ * 消息结束事件,代表一条消息的处理结束
23
+ */
24
+ MESSAGE_END = "message_end",
25
+ /**
26
+ * TTS 消息事件,代表文本转语音的消息
27
+ */
28
+ TTS_MESSAGE = "tts_message",
29
+ /**
30
+ * TTS 消息结束事件,代表文本转语音消息的处理结束
31
+ */
32
+ TTS_MESSAGE_END = "tts_message_end",
33
+ /**
34
+ * 消息替换事件,代表对已有消息的替换操作
35
+ */
36
+ MESSAGE_REPLACE = "message_replace",
37
+ /**
38
+ * 错误事件,代表系统出现错误的情况
39
+ */
40
+ ERROR = "error",
41
+ /**
42
+ * 心跳事件,用于保持连接或检测服务状态
43
+ */
44
+ PING = "ping",
45
+ /**
46
+ * 工作流开始事件,代表工作流开始执行
47
+ */
48
+ WORKFLOW_STARTED = "workflow_started",
49
+ /**
50
+ * 工作流结束事件,代表工作流执行完成
51
+ */
52
+ WORKFLOW_FINISHED = "workflow_finished",
53
+ /**
54
+ * 工作流节点开始事件,代表工作流中的某个节点开始执行
55
+ */
56
+ WORKFLOW_NODE_STARTED = "node_started",
57
+ /**
58
+ * 工作流节点结束事件,代表工作流中的某个节点执行完成
59
+ */
60
+ WORKFLOW_NODE_FINISHED = "node_finished"
61
+ }