@tais00/core 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.
- package/README.md +172 -0
- package/dist/constants/app.d.ts +66 -0
- package/dist/constants/index.d.ts +35 -0
- package/dist/enums/index.d.ts +13 -0
- package/dist/hooks/index.d.ts +3 -0
- package/dist/hooks/use-apps.d.ts +31 -0
- package/dist/hooks/use-conversations.d.ts +40 -0
- package/dist/hooks/use-dify-chat.d.ts +63 -0
- package/dist/index.cjs +3956 -0
- package/dist/index.cjs.LICENSE.txt +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +3849 -0
- package/dist/index.js.LICENSE.txt +1 -0
- package/dist/repository/app/index.d.ts +158 -0
- package/dist/repository/index.d.ts +1 -0
- package/dist/types/index.d.ts +227 -0
- package/dist/utils/index.d.ts +6 -0
- package/package.json +60 -0
package/README.md
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# `@dify-chat/core`
|
|
2
|
+
|
|
3
|
+
  
|
|
4
|
+
|
|
5
|
+
`@dify-chat/core` 是 [Dify Chat](https://github.com/lexmin0412/dify-chat) 项目中的核心包,它提供了应用、对话等全局上下文的注入和获取功能。
|
|
6
|
+
|
|
7
|
+
下面将介绍如何在你的应用中集成和使用它。
|
|
8
|
+
|
|
9
|
+
## 安装
|
|
10
|
+
|
|
11
|
+
通过 npm/yarn/pnpm 安装:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
# npm
|
|
15
|
+
npm install @dify-chat/core
|
|
16
|
+
# yarn
|
|
17
|
+
yarn add @dify-chat/core
|
|
18
|
+
# pnpm
|
|
19
|
+
pnpm add @dify-chat/core
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## 使用方法
|
|
23
|
+
|
|
24
|
+
### 全局上下文
|
|
25
|
+
|
|
26
|
+
#### AppContext
|
|
27
|
+
|
|
28
|
+
`AppContext` 是应用上下文,提供了当前应用配置的获取和更新功能。
|
|
29
|
+
|
|
30
|
+
**AppContextProvider**
|
|
31
|
+
|
|
32
|
+
在应用切换功能的最上层组件中使用 `AppContextProvider` 提供应用上下文:
|
|
33
|
+
|
|
34
|
+
```tsx
|
|
35
|
+
import { AppContextProvider, ICurrentApp } from '@dify-chat/core';
|
|
36
|
+
import { createDifyApiInstance } from '@dify-chat/api';
|
|
37
|
+
import { generateUuidV4 } from '@dify-chat/helpers'
|
|
38
|
+
|
|
39
|
+
const YourChatComponent = () => {
|
|
40
|
+
|
|
41
|
+
const { user } = useDifyChat();
|
|
42
|
+
const [appList, setAppList] = useState<ICurrentApp[]>([])
|
|
43
|
+
|
|
44
|
+
// 实现获取应用列表的逻辑
|
|
45
|
+
const getAppList = async () => {
|
|
46
|
+
setAppList([...])
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const [appLoading, setAppLoading] = useState(true)
|
|
50
|
+
const [ currentApp, setCurrentApp ] = useState<ICurrentApp[]>([]);
|
|
51
|
+
const [currentAppId, setCurrentAppId] = useState('')
|
|
52
|
+
const [difyApi] = useState(() => createDifyApiInstance({
|
|
53
|
+
user,
|
|
54
|
+
apiBase: '',
|
|
55
|
+
apiKey: '',
|
|
56
|
+
}))
|
|
57
|
+
|
|
58
|
+
// 定义获取应用参数的函数
|
|
59
|
+
const getAppInfo = async() => {
|
|
60
|
+
// 先更新 difyApi 的参数
|
|
61
|
+
difyApi.updateOptions({
|
|
62
|
+
user,
|
|
63
|
+
apiBase: newApp.requestConfig.apiBase,
|
|
64
|
+
apiKey: newApp.requestConfig.apiKey,
|
|
65
|
+
})
|
|
66
|
+
setAppLoading(true)
|
|
67
|
+
// 根据新的 currentAppId 获取新的应用信息
|
|
68
|
+
const appConfig = appList.find(item => item.id === currentAppId)
|
|
69
|
+
const difyAppInfo = await difyApi.getAppInfo()
|
|
70
|
+
const appParameters = await getAppParameters(difyApi)
|
|
71
|
+
setAppLoading(false)
|
|
72
|
+
setCurrentApp({
|
|
73
|
+
config: {
|
|
74
|
+
id: generateUuidV4(),
|
|
75
|
+
info: difyAppInfo,
|
|
76
|
+
requestConfig: appConfig.requestConfig,
|
|
77
|
+
answerForm: appConfig.answerForm,
|
|
78
|
+
},
|
|
79
|
+
parameters: appParameters,
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// 初始化时获取应用列表
|
|
84
|
+
useEffect(()=>{
|
|
85
|
+
getAppList()
|
|
86
|
+
}, [])
|
|
87
|
+
|
|
88
|
+
// 监听 currentAppId 变化,更新当前应用配置
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
updateAppInfo()
|
|
91
|
+
}, [currentAppId])
|
|
92
|
+
|
|
93
|
+
return (
|
|
94
|
+
<AppContextProvider
|
|
95
|
+
value={{
|
|
96
|
+
appLoading,
|
|
97
|
+
currentAppId,
|
|
98
|
+
setCurrentAppId,
|
|
99
|
+
currentApp: currentApp,
|
|
100
|
+
setCurrentApp,
|
|
101
|
+
}}
|
|
102
|
+
>
|
|
103
|
+
Your Chat Inner Component
|
|
104
|
+
<Button onClick={()=>setCurrentAppId('new-app-id')}>切换应用</Button>
|
|
105
|
+
</>
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
**useAppContext hook**
|
|
111
|
+
|
|
112
|
+
在你的子组件中使用 `useAppContext` 钩子获取应用上下文:
|
|
113
|
+
|
|
114
|
+
```tsx
|
|
115
|
+
import { useAppContext } from '@dify-chat/core'
|
|
116
|
+
|
|
117
|
+
const YourInnerComponent = () => {
|
|
118
|
+
const { currentApp, currentAppId } = useAppContext()
|
|
119
|
+
console.log(`当前应用ID:${currentAppId}`, `当前应用:${currentApp}`)
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
#### 对话上下文
|
|
124
|
+
|
|
125
|
+
`ConversationContext` 是对话上下文,提供了对话相关的功能,包括获取/更新对话列表、获取/更新当前对话 ID、获取当前对话信息等。
|
|
126
|
+
|
|
127
|
+
**ConversationContextProvider**
|
|
128
|
+
|
|
129
|
+
在具备对话切换功能的最上层组件中使用 `ConversationContextProvider` 提供对话上下文:
|
|
130
|
+
|
|
131
|
+
```tsx
|
|
132
|
+
import { ConversationsContextProvider } from '@dify-chat/core';
|
|
133
|
+
|
|
134
|
+
const YourChatComponent = () => {
|
|
135
|
+
const { user } = useDifyChat();
|
|
136
|
+
const [conversations, setConversations] = useState([])
|
|
137
|
+
const [currentConversationId, setCurrentConversationId] = useState('')
|
|
138
|
+
|
|
139
|
+
// 实现获取对话列表的逻辑
|
|
140
|
+
const listConversations = async () => {
|
|
141
|
+
setConversations([...])
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
useEffect(()=>{
|
|
145
|
+
listConversations()
|
|
146
|
+
}, [])
|
|
147
|
+
|
|
148
|
+
return (
|
|
149
|
+
<ConversationsContextProvider value={{
|
|
150
|
+
conversations,
|
|
151
|
+
setConversations,
|
|
152
|
+
currentConversationId,
|
|
153
|
+
setCurrentConversationId,
|
|
154
|
+
}}>
|
|
155
|
+
<YourInnerComponent />
|
|
156
|
+
</ConversationContextProvider>
|
|
157
|
+
)
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
**`useConversationsContext` hook**
|
|
162
|
+
|
|
163
|
+
在你的子组件中使用 `useConversationsContext` 钩子获取对话上下文:
|
|
164
|
+
|
|
165
|
+
```tsx
|
|
166
|
+
import { useConversationsContext } from '@dify-chat/core'
|
|
167
|
+
|
|
168
|
+
const YourInnerComponent = () => {
|
|
169
|
+
const { conversations, currentConversationId } = useConversationsContext()
|
|
170
|
+
console.log(`当前对话ID:${currentConversationId}`, `对话列表:${conversations}`)
|
|
171
|
+
}
|
|
172
|
+
```
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 应用类型
|
|
3
|
+
*/
|
|
4
|
+
export declare enum AppModeEnums {
|
|
5
|
+
/**
|
|
6
|
+
* 文本生成
|
|
7
|
+
*/
|
|
8
|
+
TEXT_GENERATOR = "completion",
|
|
9
|
+
/**
|
|
10
|
+
* 聊天助手
|
|
11
|
+
*/
|
|
12
|
+
CHATBOT = "chat",
|
|
13
|
+
/**
|
|
14
|
+
* 工作流
|
|
15
|
+
*/
|
|
16
|
+
WORKFLOW = "workflow",
|
|
17
|
+
/**
|
|
18
|
+
* 支持工作流编排的聊天助手
|
|
19
|
+
*/
|
|
20
|
+
CHATFLOW = "advanced-chat",
|
|
21
|
+
/**
|
|
22
|
+
* 具备推理和自主调用能力的聊天助手
|
|
23
|
+
*/
|
|
24
|
+
AGENT = "agent-chat"
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 应用类型的展示标签
|
|
28
|
+
*/
|
|
29
|
+
export declare const AppModeLabels: {
|
|
30
|
+
completion: string;
|
|
31
|
+
chat: string;
|
|
32
|
+
workflow: string;
|
|
33
|
+
"advanced-chat": string;
|
|
34
|
+
"agent-chat": string;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* 应用类型的展示名称
|
|
38
|
+
*/
|
|
39
|
+
export declare const AppModeNames: {
|
|
40
|
+
completion: string;
|
|
41
|
+
chat: string;
|
|
42
|
+
workflow: string;
|
|
43
|
+
"advanced-chat": string;
|
|
44
|
+
"agent-chat": string;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* 应用类型选项
|
|
48
|
+
*/
|
|
49
|
+
export declare const AppModeOptions: {
|
|
50
|
+
label: string;
|
|
51
|
+
value: AppModeEnums;
|
|
52
|
+
}[];
|
|
53
|
+
/**
|
|
54
|
+
* 开场白展示模式
|
|
55
|
+
*/
|
|
56
|
+
export declare const OpeningStatementDisplayMode: {
|
|
57
|
+
Default: string;
|
|
58
|
+
Always: string;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* 开场白展示模式选项
|
|
62
|
+
*/
|
|
63
|
+
export declare const OpeningStatementDisplayModeOptions: {
|
|
64
|
+
label: string;
|
|
65
|
+
value: string;
|
|
66
|
+
}[];
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export * from './app';
|
|
2
|
+
/**
|
|
3
|
+
* 消息角色
|
|
4
|
+
*/
|
|
5
|
+
export type IMessageRole = 'local' | 'user' | 'ai';
|
|
6
|
+
/**
|
|
7
|
+
* 聊天中的角色
|
|
8
|
+
*/
|
|
9
|
+
export declare const Roles: {
|
|
10
|
+
/**
|
|
11
|
+
* 用户
|
|
12
|
+
*/
|
|
13
|
+
readonly USER: "user";
|
|
14
|
+
/**
|
|
15
|
+
* AI
|
|
16
|
+
*/
|
|
17
|
+
readonly AI: "ai";
|
|
18
|
+
/**
|
|
19
|
+
* 本地,用户已发送但还未收到响应
|
|
20
|
+
*/
|
|
21
|
+
readonly LOCAL: "local";
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* 运行模式
|
|
25
|
+
*/
|
|
26
|
+
export declare const RunningModes: {
|
|
27
|
+
/**
|
|
28
|
+
* 单应用
|
|
29
|
+
*/
|
|
30
|
+
readonly SingleApp: "singleApp";
|
|
31
|
+
/**
|
|
32
|
+
* 多应用
|
|
33
|
+
*/
|
|
34
|
+
readonly MultiApp: "multiApp";
|
|
35
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { IDifyAppItem } from '../repository';
|
|
3
|
+
import { IDifyAppParameters, IDifyAppSiteSetting } from '../types';
|
|
4
|
+
export interface ICurrentApp {
|
|
5
|
+
config: IDifyAppItem;
|
|
6
|
+
/**
|
|
7
|
+
* 应用参数
|
|
8
|
+
*/
|
|
9
|
+
parameters: IDifyAppParameters;
|
|
10
|
+
/**
|
|
11
|
+
* 应用 WebApp 设置, 需要 Dify >= 1.4.0
|
|
12
|
+
*/
|
|
13
|
+
site?: IDifyAppSiteSetting;
|
|
14
|
+
}
|
|
15
|
+
export interface IAppContext {
|
|
16
|
+
appLoading?: boolean;
|
|
17
|
+
currentAppId?: string;
|
|
18
|
+
setCurrentAppId: (appId: string) => void;
|
|
19
|
+
currentApp?: ICurrentApp;
|
|
20
|
+
setCurrentApp: (app: ICurrentApp | undefined) => void;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 应用的默认 WebApp 设置
|
|
24
|
+
*/
|
|
25
|
+
export declare const DEFAULT_APP_SITE_SETTING: IDifyAppSiteSetting;
|
|
26
|
+
export declare const AppContext: React.Context<IAppContext>;
|
|
27
|
+
export declare const AppContextProvider: React.Provider<IAppContext>;
|
|
28
|
+
/**
|
|
29
|
+
* 获取应用上下文
|
|
30
|
+
*/
|
|
31
|
+
export declare const useAppContext: () => IAppContext;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
interface IConversationItem {
|
|
3
|
+
created_at: number;
|
|
4
|
+
id: string;
|
|
5
|
+
inputs: Record<string, unknown>;
|
|
6
|
+
introduction: string;
|
|
7
|
+
name: string;
|
|
8
|
+
status: 'normal';
|
|
9
|
+
updated_at: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 对话上下文类型
|
|
13
|
+
*/
|
|
14
|
+
export interface IConversationContext {
|
|
15
|
+
conversations: IConversationItem[];
|
|
16
|
+
setConversations: React.Dispatch<React.SetStateAction<IConversationItem[]>>;
|
|
17
|
+
currentConversationId: string;
|
|
18
|
+
setCurrentConversationId: React.Dispatch<React.SetStateAction<string>>;
|
|
19
|
+
currentConversationInfo?: IConversationItem;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 对话上下文
|
|
23
|
+
*/
|
|
24
|
+
export declare const ConversationContext: React.Context<IConversationContext>;
|
|
25
|
+
/**
|
|
26
|
+
* 对话上下文 Provoder
|
|
27
|
+
*/
|
|
28
|
+
export declare const ConversationsContextProvider: React.Provider<IConversationContext>;
|
|
29
|
+
/**
|
|
30
|
+
* 获取对话上下文
|
|
31
|
+
*/
|
|
32
|
+
export declare function useConversationsContext(): IConversationContext;
|
|
33
|
+
export declare const useConversations: () => {
|
|
34
|
+
conversations: IConversationItem[];
|
|
35
|
+
setConversations: React.Dispatch<React.SetStateAction<IConversationItem[]>>;
|
|
36
|
+
currentConversationId: string | undefined;
|
|
37
|
+
setCurrentConversationId: React.Dispatch<React.SetStateAction<string | undefined>>;
|
|
38
|
+
currentConversationInfo: IConversationItem | undefined;
|
|
39
|
+
};
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { AppModeEnums } from '../constants';
|
|
3
|
+
import { DifyAppStore, DifyAppStoreReadonly, IDifyAppItem } from '../repository';
|
|
4
|
+
export type IDifyChatMode = 'singleApp' | 'multiApp';
|
|
5
|
+
interface IDifyChatContextBase {
|
|
6
|
+
/**
|
|
7
|
+
* 当前用户
|
|
8
|
+
*/
|
|
9
|
+
user: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 单应用模式下的上下文
|
|
13
|
+
*/
|
|
14
|
+
export interface IDifyChatContextSingleApp extends IDifyChatContextBase {
|
|
15
|
+
/**
|
|
16
|
+
* 交互模式 - 单应用
|
|
17
|
+
*/
|
|
18
|
+
mode: 'singleApp';
|
|
19
|
+
/**
|
|
20
|
+
* 当前应用配置
|
|
21
|
+
*/
|
|
22
|
+
appConfig: Omit<IDifyAppItem, 'id' | 'info'> & {
|
|
23
|
+
/**
|
|
24
|
+
* 应用信息 可选,主要是为了兼容旧版本 dify(<=1.3.1) 的 /info 接口没有返回 mode 的情况
|
|
25
|
+
*/
|
|
26
|
+
info?: {
|
|
27
|
+
/**
|
|
28
|
+
* 应用类型
|
|
29
|
+
*/
|
|
30
|
+
mode?: AppModeEnums;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* 多应用模式下的上下文
|
|
36
|
+
*/
|
|
37
|
+
export interface IDifyChatContextMultiApp extends IDifyChatContextBase {
|
|
38
|
+
/**
|
|
39
|
+
* 交互模式 - 多应用
|
|
40
|
+
*/
|
|
41
|
+
mode: 'multiApp';
|
|
42
|
+
/**
|
|
43
|
+
* 应用服务,用于实现应用列表的 CRUD 管理
|
|
44
|
+
*/
|
|
45
|
+
appService: DifyAppStore | DifyAppStoreReadonly;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* DifyChat 上下文类型
|
|
49
|
+
*/
|
|
50
|
+
type IDifyChatContext = IDifyChatContextSingleApp | IDifyChatContextMultiApp;
|
|
51
|
+
/**
|
|
52
|
+
* DifyChat 全局上下文
|
|
53
|
+
*/
|
|
54
|
+
export declare const DifyChatContext: React.Context<IDifyChatContext>;
|
|
55
|
+
/**
|
|
56
|
+
* DifyChatContext 的 Provider
|
|
57
|
+
*/
|
|
58
|
+
export declare const DifyChatProvider: React.Provider<IDifyChatContext>;
|
|
59
|
+
/**
|
|
60
|
+
* 使用 DifyChat 的 context 值
|
|
61
|
+
*/
|
|
62
|
+
export declare const useDifyChat: () => IDifyChatContext;
|
|
63
|
+
export {};
|