@unif/react-native-chat-sdk 0.4.0 → 0.4.2
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/commonjs/hooks/useXConversations.js.map +1 -1
- package/lib/commonjs/hooks/useXModel.js.map +1 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/providers/AbstractChatProvider.js.map +1 -1
- package/lib/commonjs/providers/DeepSeekChatProvider.js.map +1 -1
- package/lib/commonjs/providers/OpenAIChatProvider.js +1 -1
- package/lib/commonjs/providers/OpenAIChatProvider.js.map +1 -1
- package/lib/commonjs/stores/chatStore.js.map +1 -1
- package/lib/commonjs/stores/historyStore.js.map +1 -1
- package/lib/commonjs/tools/XRequest.js +1 -1
- package/lib/commonjs/tools/XRequest.js.map +1 -1
- package/lib/commonjs/tools/XStream.js +0 -1
- package/lib/commonjs/tools/XStream.js.map +1 -1
- package/lib/module/hooks/useXConversations.js.map +1 -1
- package/lib/module/hooks/useXModel.js.map +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/providers/AbstractChatProvider.js.map +1 -1
- package/lib/module/providers/DeepSeekChatProvider.js.map +1 -1
- package/lib/module/providers/OpenAIChatProvider.js +1 -1
- package/lib/module/providers/OpenAIChatProvider.js.map +1 -1
- package/lib/module/stores/chatStore.js.map +1 -1
- package/lib/module/stores/historyStore.js.map +1 -1
- package/lib/module/stores/modelStore.js.map +1 -1
- package/lib/module/stores/sessionStore.js.map +1 -1
- package/lib/module/tools/XRequest.js +1 -1
- package/lib/module/tools/XRequest.js.map +1 -1
- package/lib/module/tools/XStream.js +0 -1
- package/lib/module/tools/XStream.js.map +1 -1
- package/lib/typescript/commonjs/hooks/useXConversations.d.ts.map +1 -1
- package/lib/typescript/commonjs/index.d.ts +2 -2
- package/lib/typescript/commonjs/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/providers/AbstractChatProvider.d.ts.map +1 -1
- package/lib/typescript/commonjs/providers/DeepSeekChatProvider.d.ts.map +1 -1
- package/lib/typescript/commonjs/providers/OpenAIChatProvider.d.ts.map +1 -1
- package/lib/typescript/commonjs/stores/chatStore.d.ts.map +1 -1
- package/lib/typescript/commonjs/stores/historyStore.d.ts.map +1 -1
- package/lib/typescript/commonjs/tools/SSEParser.d.ts.map +1 -1
- package/lib/typescript/commonjs/tools/XStream.d.ts.map +1 -1
- package/lib/typescript/commonjs/types/provider.d.ts.map +1 -1
- package/lib/typescript/module/hooks/useXConversations.d.ts.map +1 -1
- package/lib/typescript/module/index.d.ts +2 -2
- package/lib/typescript/module/index.d.ts.map +1 -1
- package/lib/typescript/module/providers/AbstractChatProvider.d.ts.map +1 -1
- package/lib/typescript/module/providers/DeepSeekChatProvider.d.ts.map +1 -1
- package/lib/typescript/module/providers/OpenAIChatProvider.d.ts.map +1 -1
- package/lib/typescript/module/stores/chatStore.d.ts.map +1 -1
- package/lib/typescript/module/stores/historyStore.d.ts.map +1 -1
- package/lib/typescript/module/tools/SSEParser.d.ts.map +1 -1
- package/lib/typescript/module/tools/XStream.d.ts.map +1 -1
- package/lib/typescript/module/types/provider.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/hooks/ChatProvider.tsx +3 -3
- package/src/hooks/useXChat.ts +3 -3
- package/src/hooks/useXConversations.ts +6 -6
- package/src/hooks/useXModel.ts +2 -2
- package/src/index.ts +26 -19
- package/src/providers/AbstractChatProvider.ts +7 -3
- package/src/providers/DeepSeekChatProvider.ts +4 -7
- package/src/providers/OpenAIChatProvider.ts +6 -6
- package/src/stores/chatStore.ts +9 -10
- package/src/stores/historyStore.ts +4 -8
- package/src/stores/modelStore.ts +1 -1
- package/src/stores/sessionStore.ts +1 -1
- package/src/tools/SSEParser.ts +1 -1
- package/src/tools/XRequest.ts +1 -1
- package/src/tools/XStream.ts +0 -1
- package/src/types/provider.ts +2 -2
- package/lib/commonjs/index.md +0 -188
- package/lib/module/index.md +0 -188
- package/src/index.md +0 -188
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chatStore.d.ts","sourceRoot":"","sources":["../../../../src/stores/chatStore.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"chatStore.d.ts","sourceRoot":"","sources":["../../../../src/stores/chatStore.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAEnD,UAAU,SAAS;IACjB,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,cAAc,EAAE,CAAC;IAC9B,YAAY,EAAE,OAAO,CAAC;IACtB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAErB,UAAU,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,IAAI,CAAC;IAC3C,aAAa,EAAE,CACb,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,WAAW,KACvC,IAAI,CAAC;IACV,aAAa,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,IAAI,CAAC;IAC9C,WAAW,EAAE,CACX,QAAQ,EAAE,WAAW,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,WAAW,EAAE,KAAK,WAAW,EAAE,CAAC,KAC/D,IAAI,CAAC;IACV,UAAU,EAAE,CACV,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,OAAO,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,WAAW,KAAK,OAAO,CAAC,WAAW,CAAC,CAAC,KACzE,IAAI,CAAC;IACV,aAAa,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAC;IACpC,cAAc,EAAE,CAAC,KAAK,EAAE,cAAc,EAAE,KAAK,IAAI,CAAC;IAClD,aAAa,EAAE,CAAC,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;IACpC,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,KAAK,IAAI,CAAC;IACzC,SAAS,EAAE,MAAM,IAAI,CAAC;IACtB,WAAW,EAAE,CAAC,QAAQ,EAAE,WAAW,EAAE,KAAK,IAAI,CAAC;CAChD;AAED,eAAO,MAAM,eAAe,8EA2EvB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"historyStore.d.ts","sourceRoot":"","sources":["../../../../src/stores/historyStore.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"historyStore.d.ts","sourceRoot":"","sources":["../../../../src/stores/historyStore.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAK7C,MAAM,WAAW,cAAc;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,cAAc,EAAE,CAAC;IAC3B,cAAc,EAAE,CACd,EAAE,EAAE,MAAM,EACV,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,WAAW,EAAE,KACpB,OAAO,CAAC,IAAI,CAAC,CAAC;IACnB,mBAAmB,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;IAC5D,aAAa,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7C,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/B;AAED,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,WAAW,GAAG,YAAY,CAiFrE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SSEParser.d.ts","sourceRoot":"","sources":["../../../../src/tools/SSEParser.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"SSEParser.d.ts","sourceRoot":"","sources":["../../../../src/tools/SSEParser.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEhD,qBAAa,SAAS;IACpB,OAAO,CAAC,YAAY,CAAqB;IACzC,OAAO,CAAC,MAAM,CAAkC;IAChD,OAAO,CAAC,OAAO,CAAK;IAEpB,KAAK,IAAI,IAAI;IAMb;;;OAGG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,WAAW,EAAE;CA2BpC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"XStream.d.ts","sourceRoot":"","sources":["../../../../src/tools/XStream.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAInD,MAAM,WAAW,aAAa;IAC5B,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,eAAe,CAAC,EAAE,CAAC,KAAK,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,KAAK,SAAS,CAAC;CAC1E;AAED,MAAM,WAAW,gBAAgB;IAC/B,SAAS,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAC;IACrC,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IACjC,UAAU,CAAC,EAAE,MAAM,IAAI,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,IAAI,CAAC;CACrB;AAUD,qBAAa,OAAO;IAClB,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,EAAE,CAA4B;IACtC,OAAO,CAAC,YAAY,CAA8C;IAClE,OAAO,CAAC,QAAQ,CAAK;IACrB,OAAO,CAAC,WAAW,CAAK;gBAEZ,MAAM,EAAE,aAAa;IAIjC,OAAO,CAAC,SAAS,EAAE,gBAAgB,GAAG,IAAI;
|
|
1
|
+
{"version":3,"file":"XStream.d.ts","sourceRoot":"","sources":["../../../../src/tools/XStream.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAInD,MAAM,WAAW,aAAa;IAC5B,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,eAAe,CAAC,EAAE,CAAC,KAAK,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,KAAK,SAAS,CAAC;CAC1E;AAED,MAAM,WAAW,gBAAgB;IAC/B,SAAS,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAC;IACrC,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IACjC,UAAU,CAAC,EAAE,MAAM,IAAI,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,IAAI,CAAC;CACrB;AAUD,qBAAa,OAAO;IAClB,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,EAAE,CAA4B;IACtC,OAAO,CAAC,YAAY,CAA8C;IAClE,OAAO,CAAC,QAAQ,CAAK;IACrB,OAAO,CAAC,WAAW,CAAK;gBAEZ,MAAM,EAAE,aAAa;IAIjC,OAAO,CAAC,SAAS,EAAE,gBAAgB,GAAG,IAAI;IA8E1C,KAAK,IAAI,IAAI;IAYb,OAAO,IAAI,IAAI;IAIf,OAAO,CAAC,UAAU;CAMnB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../../../src/types/provider.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../../../src/types/provider.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AAE5C,MAAM,WAAW,aAAa,CAAC,CAAC,GAAG,WAAW;IAC5C,OAAO,EAAE,CAAC,CAAC;IACX,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACjC;AAED,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,iBAAiB,CAAC,OAAO,GAAG,WAAW;IACtD,QAAQ,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;IACrC,SAAS,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;IACtC,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IAChC,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,cAAc,EAAE,KAAK,IAAI,CAAC;CAClD"}
|
package/package.json
CHANGED
|
@@ -23,7 +23,7 @@ import type { AbstractChatProvider } from '../providers/AbstractChatProvider';
|
|
|
23
23
|
export interface ChatProviderProps<
|
|
24
24
|
Message = ChatMessage,
|
|
25
25
|
Input = RequestParams<Message>,
|
|
26
|
-
Output = SSEOutput
|
|
26
|
+
Output = SSEOutput,
|
|
27
27
|
> {
|
|
28
28
|
provider: AbstractChatProvider<Message, Input, Output>;
|
|
29
29
|
storage?: ChatStorage;
|
|
@@ -95,7 +95,7 @@ const memoryStorage: ChatStorage = {
|
|
|
95
95
|
export function ChatProvider<
|
|
96
96
|
Message = ChatMessage,
|
|
97
97
|
Input = RequestParams<Message>,
|
|
98
|
-
Output = SSEOutput
|
|
98
|
+
Output = SSEOutput,
|
|
99
99
|
>(props: ChatProviderProps<Message, Input, Output>) {
|
|
100
100
|
const {
|
|
101
101
|
provider,
|
|
@@ -145,7 +145,7 @@ export function ChatProvider<
|
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
export function useChatContext<
|
|
148
|
-
Message = ChatMessage
|
|
148
|
+
Message = ChatMessage,
|
|
149
149
|
>(): ChatContextValue<Message> {
|
|
150
150
|
const ctx = useContext(ChatContext);
|
|
151
151
|
if (!ctx) {
|
package/src/hooks/useXChat.ts
CHANGED
|
@@ -21,7 +21,7 @@ export interface MessageInfo<Message = ChatMessage> {
|
|
|
21
21
|
export interface UseXChatConfig<
|
|
22
22
|
Message = ChatMessage,
|
|
23
23
|
Input = RequestParams<Message>,
|
|
24
|
-
Output = SSEOutput
|
|
24
|
+
Output = SSEOutput,
|
|
25
25
|
> {
|
|
26
26
|
provider: AbstractChatProvider<Message, Input, Output>;
|
|
27
27
|
requestPlaceholder?: (input: Input) => Message;
|
|
@@ -31,7 +31,7 @@ export interface UseXChatConfig<
|
|
|
31
31
|
|
|
32
32
|
export interface UseXChatReturn<
|
|
33
33
|
Message = ChatMessage,
|
|
34
|
-
Input = RequestParams<Message
|
|
34
|
+
Input = RequestParams<Message>,
|
|
35
35
|
> {
|
|
36
36
|
messages: MessageInfo<Message>[];
|
|
37
37
|
requesting: boolean;
|
|
@@ -60,7 +60,7 @@ export interface UseXChatReturn<
|
|
|
60
60
|
export function useXChat<
|
|
61
61
|
Message = ChatMessage,
|
|
62
62
|
Input = RequestParams<Message>,
|
|
63
|
-
Output = SSEOutput
|
|
63
|
+
Output = SSEOutput,
|
|
64
64
|
>(
|
|
65
65
|
config: UseXChatConfig<Message, Input, Output>
|
|
66
66
|
): UseXChatReturn<Message, Input> {
|
|
@@ -4,12 +4,12 @@
|
|
|
4
4
|
* 组合 sessionStore + historyStore,管理会话生命周期
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import {useState, useCallback, useEffect, useRef} from 'react';
|
|
8
|
-
import {createHistoryStore} from '../stores/historyStore';
|
|
9
|
-
import type {ChatStorage} from '../stores/storage';
|
|
10
|
-
import type {ChatMessage} from '../types/message';
|
|
7
|
+
import { useState, useCallback, useEffect, useRef } from 'react';
|
|
8
|
+
import { createHistoryStore } from '../stores/historyStore';
|
|
9
|
+
import type { ChatStorage } from '../stores/storage';
|
|
10
|
+
import type { ChatMessage } from '../types/message';
|
|
11
11
|
|
|
12
|
-
export {type SessionSummary} from '../stores/historyStore';
|
|
12
|
+
export { type SessionSummary } from '../stores/historyStore';
|
|
13
13
|
|
|
14
14
|
export interface UseXConversationsOptions {
|
|
15
15
|
storage: ChatStorage;
|
|
@@ -17,7 +17,7 @@ export interface UseXConversationsOptions {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
export function useXConversations(options: UseXConversationsOptions) {
|
|
20
|
-
const {storage} = options;
|
|
20
|
+
const { storage } = options;
|
|
21
21
|
|
|
22
22
|
const historyRef = useRef(createHistoryStore(storage));
|
|
23
23
|
const history = historyRef.current;
|
package/src/hooks/useXModel.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* useXModel — 模型选择 hook
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import {useState, useCallback} from 'react';
|
|
5
|
+
import { useState, useCallback } from 'react';
|
|
6
6
|
|
|
7
7
|
export interface ModelInfo {
|
|
8
8
|
id: string;
|
|
@@ -16,7 +16,7 @@ export interface UseXModelOptions {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export function useXModel(options: UseXModelOptions) {
|
|
19
|
-
const {models, defaultModel} = options;
|
|
19
|
+
const { models, defaultModel } = options;
|
|
20
20
|
const [selectedModel, setSelected] = useState(
|
|
21
21
|
defaultModel ?? models[0]?.id ?? ''
|
|
22
22
|
);
|
package/src/index.ts
CHANGED
|
@@ -30,33 +30,40 @@ export type {
|
|
|
30
30
|
} from './types/provider';
|
|
31
31
|
|
|
32
32
|
// Tools
|
|
33
|
-
export {XStream} from './tools/XStream';
|
|
34
|
-
export type {XStreamConfig, XStreamCallbacks} from './tools/XStream';
|
|
33
|
+
export { XStream } from './tools/XStream';
|
|
34
|
+
export type { XStreamConfig, XStreamCallbacks } from './tools/XStream';
|
|
35
35
|
|
|
36
|
-
export {XRequest} from './tools/XRequest';
|
|
37
|
-
export type {XRequestConfig, XRequestCallbacks} from './tools/XRequest';
|
|
36
|
+
export { XRequest } from './tools/XRequest';
|
|
37
|
+
export type { XRequestConfig, XRequestCallbacks } from './tools/XRequest';
|
|
38
38
|
|
|
39
|
-
export {SSEParser} from './tools/SSEParser';
|
|
39
|
+
export { SSEParser } from './tools/SSEParser';
|
|
40
40
|
|
|
41
41
|
// Providers
|
|
42
|
-
export {AbstractChatProvider} from './providers/AbstractChatProvider';
|
|
43
|
-
export {OpenAIChatProvider} from './providers/OpenAIChatProvider';
|
|
44
|
-
export type {OpenAIChatProviderConfig} from './providers/OpenAIChatProvider';
|
|
45
|
-
export {DeepSeekChatProvider} from './providers/DeepSeekChatProvider';
|
|
46
|
-
export type {DeepSeekChatProviderConfig} from './providers/DeepSeekChatProvider';
|
|
42
|
+
export { AbstractChatProvider } from './providers/AbstractChatProvider';
|
|
43
|
+
export { OpenAIChatProvider } from './providers/OpenAIChatProvider';
|
|
44
|
+
export type { OpenAIChatProviderConfig } from './providers/OpenAIChatProvider';
|
|
45
|
+
export { DeepSeekChatProvider } from './providers/DeepSeekChatProvider';
|
|
46
|
+
export type { DeepSeekChatProviderConfig } from './providers/DeepSeekChatProvider';
|
|
47
47
|
|
|
48
48
|
// Stores
|
|
49
|
-
export type {ChatStorage} from './stores/storage';
|
|
49
|
+
export type { ChatStorage } from './stores/storage';
|
|
50
50
|
|
|
51
51
|
// Hooks
|
|
52
|
-
export {useXChat} from './hooks/useXChat';
|
|
53
|
-
export type {
|
|
52
|
+
export { useXChat } from './hooks/useXChat';
|
|
53
|
+
export type {
|
|
54
|
+
UseXChatConfig,
|
|
55
|
+
UseXChatReturn,
|
|
56
|
+
MessageInfo,
|
|
57
|
+
} from './hooks/useXChat';
|
|
54
58
|
|
|
55
|
-
export {useXConversations} from './hooks/useXConversations';
|
|
56
|
-
export type {
|
|
59
|
+
export { useXConversations } from './hooks/useXConversations';
|
|
60
|
+
export type {
|
|
61
|
+
UseXConversationsOptions,
|
|
62
|
+
SessionSummary,
|
|
63
|
+
} from './hooks/useXConversations';
|
|
57
64
|
|
|
58
|
-
export {useXModel} from './hooks/useXModel';
|
|
59
|
-
export type {UseXModelOptions, ModelInfo} from './hooks/useXModel';
|
|
65
|
+
export { useXModel } from './hooks/useXModel';
|
|
66
|
+
export type { UseXModelOptions, ModelInfo } from './hooks/useXModel';
|
|
60
67
|
|
|
61
|
-
export {ChatProvider, useChatContext} from './hooks/ChatProvider';
|
|
62
|
-
export type {ChatProviderProps} from './hooks/ChatProvider';
|
|
68
|
+
export { ChatProvider, useChatContext } from './hooks/ChatProvider';
|
|
69
|
+
export type { ChatProviderProps } from './hooks/ChatProvider';
|
|
@@ -4,9 +4,13 @@
|
|
|
4
4
|
* 3 个抽象方法:transformParams / transformLocalMessage / transformMessage
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import type {XRequest, XRequestCallbacks} from '../tools/XRequest';
|
|
8
|
-
import type {ChatMessage} from '../types/message';
|
|
9
|
-
import type {
|
|
7
|
+
import type { XRequest, XRequestCallbacks } from '../tools/XRequest';
|
|
8
|
+
import type { ChatMessage } from '../types/message';
|
|
9
|
+
import type {
|
|
10
|
+
RequestParams,
|
|
11
|
+
SSEOutput,
|
|
12
|
+
ProviderCallbacks,
|
|
13
|
+
} from '../types/provider';
|
|
10
14
|
|
|
11
15
|
export abstract class AbstractChatProvider<
|
|
12
16
|
Message = ChatMessage,
|
|
@@ -5,9 +5,9 @@
|
|
|
5
5
|
* 额外处理 reasoning_content 字段(DeepSeek 的思考过程)→ 映射到 <think> 标签
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import {OpenAIChatProvider} from './OpenAIChatProvider';
|
|
9
|
-
import type {ChatMessage} from '../types/message';
|
|
10
|
-
import type {SSEOutput} from '../types/provider';
|
|
8
|
+
import { OpenAIChatProvider } from './OpenAIChatProvider';
|
|
9
|
+
import type { ChatMessage } from '../types/message';
|
|
10
|
+
import type { SSEOutput } from '../types/provider';
|
|
11
11
|
|
|
12
12
|
export interface DeepSeekChatProviderConfig {
|
|
13
13
|
apiKey?: string;
|
|
@@ -71,10 +71,7 @@ export class DeepSeekChatProvider extends OpenAIChatProvider {
|
|
|
71
71
|
|
|
72
72
|
if (content) {
|
|
73
73
|
// 如果有未关闭的 think 标签,先关闭
|
|
74
|
-
if (
|
|
75
|
-
newText.includes('<think>') &&
|
|
76
|
-
!newText.includes('</think>')
|
|
77
|
-
) {
|
|
74
|
+
if (newText.includes('<think>') && !newText.includes('</think>')) {
|
|
78
75
|
newText += '</think>';
|
|
79
76
|
}
|
|
80
77
|
newText += content;
|
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
* 兼容所有 OpenAI 标准 API(OpenAI、Azure、Moonshot、通义千问兼容模式等)
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import {AbstractChatProvider} from './AbstractChatProvider';
|
|
8
|
-
import {XRequest} from '../tools/XRequest';
|
|
9
|
-
import type {ChatMessage, MessageStatus} from '../types/message';
|
|
10
|
-
import type {RequestParams, SSEOutput} from '../types/provider';
|
|
7
|
+
import { AbstractChatProvider } from './AbstractChatProvider';
|
|
8
|
+
import { XRequest } from '../tools/XRequest';
|
|
9
|
+
import type { ChatMessage, MessageStatus } from '../types/message';
|
|
10
|
+
import type { RequestParams, SSEOutput } from '../types/provider';
|
|
11
11
|
|
|
12
12
|
export interface OpenAIChatProviderConfig {
|
|
13
13
|
baseURL: string;
|
|
@@ -30,7 +30,7 @@ export class OpenAIChatProvider extends AbstractChatProvider<
|
|
|
30
30
|
constructor(config: OpenAIChatProviderConfig) {
|
|
31
31
|
const headers: Record<string, string> = {};
|
|
32
32
|
if (config.apiKey) {
|
|
33
|
-
headers
|
|
33
|
+
headers.Authorization = `Bearer ${config.apiKey}`;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
const request = new XRequest({
|
|
@@ -76,7 +76,7 @@ export class OpenAIChatProvider extends AbstractChatProvider<
|
|
|
76
76
|
createdAt: new Date(),
|
|
77
77
|
messageType: 'text',
|
|
78
78
|
status: 'local' as MessageStatus,
|
|
79
|
-
turnId: input.extra?.turnId as string ?? genId(),
|
|
79
|
+
turnId: (input.extra?.turnId as string) ?? genId(),
|
|
80
80
|
};
|
|
81
81
|
}
|
|
82
82
|
|
package/src/stores/chatStore.ts
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
* 内部 store,通过 useXChat hook 封装后对外暴露
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {create} from 'zustand';
|
|
7
|
-
import type {ChatMessage} from '../types/message';
|
|
8
|
-
import type {SuggestionItem} from '../types/sse';
|
|
6
|
+
import { create } from 'zustand';
|
|
7
|
+
import type { ChatMessage } from '../types/message';
|
|
8
|
+
import type { SuggestionItem } from '../types/sse';
|
|
9
9
|
|
|
10
10
|
interface ChatState {
|
|
11
11
|
messages: ChatMessage[];
|
|
@@ -15,18 +15,17 @@ interface ChatState {
|
|
|
15
15
|
error: string | null;
|
|
16
16
|
|
|
17
17
|
addMessage: (message: ChatMessage) => void;
|
|
18
|
-
updateMessage: (
|
|
18
|
+
updateMessage: (
|
|
19
|
+
id: string,
|
|
20
|
+
updater: (msg: ChatMessage) => ChatMessage
|
|
21
|
+
) => void;
|
|
19
22
|
upsertMessage: (message: ChatMessage) => void;
|
|
20
23
|
setMessages: (
|
|
21
|
-
messages:
|
|
22
|
-
| ChatMessage[]
|
|
23
|
-
| ((prev: ChatMessage[]) => ChatMessage[]),
|
|
24
|
+
messages: ChatMessage[] | ((prev: ChatMessage[]) => ChatMessage[])
|
|
24
25
|
) => void;
|
|
25
26
|
setMessage: (
|
|
26
27
|
id: string,
|
|
27
|
-
partial:
|
|
28
|
-
| Partial<ChatMessage>
|
|
29
|
-
| ((msg: ChatMessage) => Partial<ChatMessage>),
|
|
28
|
+
partial: Partial<ChatMessage> | ((msg: ChatMessage) => Partial<ChatMessage>)
|
|
30
29
|
) => void;
|
|
31
30
|
removeMessage: (id: string) => void;
|
|
32
31
|
setSuggestions: (items: SuggestionItem[]) => void;
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* 使用 ChatStorage 抽象,不直接依赖 AsyncStorage
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import type {ChatMessage} from '../types/message';
|
|
7
|
-
import type {ChatStorage} from './storage';
|
|
6
|
+
import type { ChatMessage } from '../types/message';
|
|
7
|
+
import type { ChatStorage } from './storage';
|
|
8
8
|
|
|
9
9
|
const MAX_SESSIONS = 50;
|
|
10
10
|
const MESSAGE_KEY_PREFIX = 'chat-session-';
|
|
@@ -81,9 +81,7 @@ export function createHistoryStore(storage: ChatStorage): HistoryStore {
|
|
|
81
81
|
if (sessions.length > MAX_SESSIONS) {
|
|
82
82
|
const removed = sessions.splice(MAX_SESSIONS);
|
|
83
83
|
await Promise.all(
|
|
84
|
-
removed.map((s) =>
|
|
85
|
-
storage.removeItem(`${MESSAGE_KEY_PREFIX}${s.id}`)
|
|
86
|
-
)
|
|
84
|
+
removed.map((s) => storage.removeItem(`${MESSAGE_KEY_PREFIX}${s.id}`))
|
|
87
85
|
);
|
|
88
86
|
}
|
|
89
87
|
|
|
@@ -106,9 +104,7 @@ export function createHistoryStore(storage: ChatStorage): HistoryStore {
|
|
|
106
104
|
clearAll: async () => {
|
|
107
105
|
await loadIndex();
|
|
108
106
|
await Promise.all(
|
|
109
|
-
sessions.map((s) =>
|
|
110
|
-
storage.removeItem(`${MESSAGE_KEY_PREFIX}${s.id}`)
|
|
111
|
-
)
|
|
107
|
+
sessions.map((s) => storage.removeItem(`${MESSAGE_KEY_PREFIX}${s.id}`))
|
|
112
108
|
);
|
|
113
109
|
sessions = [];
|
|
114
110
|
await saveIndex();
|
package/src/stores/modelStore.ts
CHANGED
package/src/tools/SSEParser.ts
CHANGED
package/src/tools/XRequest.ts
CHANGED
package/src/tools/XStream.ts
CHANGED
|
@@ -61,7 +61,6 @@ export class XStream {
|
|
|
61
61
|
console.log(TAG, '连接', url, `timeout=${timeout}ms`);
|
|
62
62
|
|
|
63
63
|
// react-native-sse 动态 require,避免非 RN 环境报错
|
|
64
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
65
64
|
const RNEventSource = require('react-native-sse').default;
|
|
66
65
|
|
|
67
66
|
const es = new RNEventSource(url, {
|
package/src/types/provider.ts
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* 定义 ChatProvider ↔ useXChat 之间的数据契约
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import type {ChatMessage} from './message';
|
|
7
|
-
import type {SuggestionItem} from './sse';
|
|
6
|
+
import type { ChatMessage } from './message';
|
|
7
|
+
import type { SuggestionItem } from './sse';
|
|
8
8
|
|
|
9
9
|
export interface RequestParams<T = ChatMessage> {
|
|
10
10
|
message: T;
|
package/lib/commonjs/index.md
DELETED
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: SDK 概览
|
|
3
|
-
nav:
|
|
4
|
-
title: SDK
|
|
5
|
-
path: /sdk
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
# @unif/react-native-chat-sdk
|
|
9
|
-
|
|
10
|
-
聊天 SDK 包,提供 Provider 抽象、Hooks、状态管理和流式处理工具。
|
|
11
|
-
|
|
12
|
-
## 架构
|
|
13
|
-
|
|
14
|
-
```
|
|
15
|
-
┌─────────────────────────────────────────┐
|
|
16
|
-
│ useXChat (Hook) │
|
|
17
|
-
│ 组合 Provider + store,输出 messages │
|
|
18
|
-
├─────────────────────────────────────────┤
|
|
19
|
-
│ ChatProvider 层 │
|
|
20
|
-
│ AbstractChatProvider → OpenAI/DeepSeek │
|
|
21
|
-
├─────────────────────────────────────────┤
|
|
22
|
-
│ 工具层 │
|
|
23
|
-
│ XRequest → XStream → SSEParser │
|
|
24
|
-
├─────────────────────────────────────────┤
|
|
25
|
-
│ 数据层 │
|
|
26
|
-
│ chatStore / sessionStore / modelStore │
|
|
27
|
-
└─────────────────────────────────────────┘
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
## 核心 Hooks
|
|
31
|
-
|
|
32
|
-
### useXChat — 聊天操作
|
|
33
|
-
|
|
34
|
-
```tsx
|
|
35
|
-
import { useXChat, OpenAIChatProvider, XRequest } from '@unif/react-native-chat-sdk';
|
|
36
|
-
|
|
37
|
-
const request = new XRequest({
|
|
38
|
-
baseURL: 'https://api.openai.com',
|
|
39
|
-
getToken: async () => 'sk-...',
|
|
40
|
-
});
|
|
41
|
-
const provider = new OpenAIChatProvider({ request, model: 'gpt-4o' });
|
|
42
|
-
|
|
43
|
-
const { messages, requesting, onRequest, abort } = useXChat({ provider });
|
|
44
|
-
|
|
45
|
-
// 发起请求
|
|
46
|
-
onRequest({ message: { text: '你好' } });
|
|
47
|
-
|
|
48
|
-
// 中止请求
|
|
49
|
-
abort();
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
**返回值:**
|
|
53
|
-
|
|
54
|
-
| 属性 | 说明 | 类型 |
|
|
55
|
-
|------|------|------|
|
|
56
|
-
| messages | 可渲染消息列表 | `MessageInfo<Message>[]` |
|
|
57
|
-
| requesting | 是否请求中 | `boolean` |
|
|
58
|
-
| suggestions | 建议提示 | `SuggestionItem[]` |
|
|
59
|
-
| onRequest | 发起请求 | `(input: Input) => void` |
|
|
60
|
-
| abort | 中止请求 | `() => void` |
|
|
61
|
-
|
|
62
|
-
### useXConversations — 会话管理
|
|
63
|
-
|
|
64
|
-
```tsx
|
|
65
|
-
import { useXConversations } from '@unif/react-native-chat-sdk';
|
|
66
|
-
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
67
|
-
|
|
68
|
-
const {
|
|
69
|
-
sessions, activeId,
|
|
70
|
-
switchSession, newSession, deleteSession,
|
|
71
|
-
} = useXConversations({ storage: AsyncStorage });
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
### useXModel — 模型选择
|
|
75
|
-
|
|
76
|
-
```tsx
|
|
77
|
-
import { useXModel } from '@unif/react-native-chat-sdk';
|
|
78
|
-
|
|
79
|
-
const { selectedModel, setSelectedModel } = useXModel({
|
|
80
|
-
models: [
|
|
81
|
-
{ id: 'gpt-4o', name: 'GPT-4o', desc: '最强模型' },
|
|
82
|
-
{ id: 'gpt-4o-mini', name: 'GPT-4o Mini', desc: '快速响应' },
|
|
83
|
-
],
|
|
84
|
-
defaultModel: 'gpt-4o',
|
|
85
|
-
});
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
## Provider 体系
|
|
89
|
-
|
|
90
|
-
### 内置 Provider
|
|
91
|
-
|
|
92
|
-
| Provider | 说明 |
|
|
93
|
-
|----------|------|
|
|
94
|
-
| `OpenAIChatProvider` | 兼容 OpenAI API 格式(含 Azure、Moonshot、通义千问等) |
|
|
95
|
-
| `DeepSeekChatProvider` | 继承 OpenAI,额外处理 `reasoning_content` 字段 |
|
|
96
|
-
|
|
97
|
-
### 自定义 Provider
|
|
98
|
-
|
|
99
|
-
继承 `AbstractChatProvider`,实现 3 个抽象方法:
|
|
100
|
-
|
|
101
|
-
```tsx
|
|
102
|
-
import { AbstractChatProvider } from '@unif/react-native-chat-sdk';
|
|
103
|
-
|
|
104
|
-
class MyProvider extends AbstractChatProvider {
|
|
105
|
-
transformParams(input) {
|
|
106
|
-
// 转换请求参数为你的 API 格式
|
|
107
|
-
return { query: input.message.text, model: input.model };
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
transformLocalMessage(input) {
|
|
111
|
-
// 创建本地用户消息
|
|
112
|
-
return { id: generateId(), text: input.message.text, role: 'user', status: 'local', ... };
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
transformMessage(output, current) {
|
|
116
|
-
// 解析流式响应,累积到 assistant 消息
|
|
117
|
-
const data = JSON.parse(output.data);
|
|
118
|
-
return { ...current, text: (current?.text || '') + data.content, status: 'updating' };
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
## 工具类
|
|
124
|
-
|
|
125
|
-
### XRequest
|
|
126
|
-
|
|
127
|
-
面向 LLM 的 HTTP 请求工具,内置 token 注入和 SSE 解析。
|
|
128
|
-
|
|
129
|
-
```tsx
|
|
130
|
-
const request = new XRequest({
|
|
131
|
-
baseURL: 'https://api.example.com',
|
|
132
|
-
endpoint: '/v1/chat/completions',
|
|
133
|
-
getToken: async () => await loadToken(),
|
|
134
|
-
timeout: 120000,
|
|
135
|
-
});
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
### XStream
|
|
139
|
-
|
|
140
|
-
SSE 流适配器(RN 原生实现,基于 react-native-sse)。
|
|
141
|
-
|
|
142
|
-
### SSEParser
|
|
143
|
-
|
|
144
|
-
SSE 解析器,提供 `event_id` 幂等去重和 `seq` 严格排序。
|
|
145
|
-
|
|
146
|
-
## ChatProvider — 全局模式
|
|
147
|
-
|
|
148
|
-
```tsx
|
|
149
|
-
import { ChatProvider, useChatContext } from '@unif/react-native-chat-sdk';
|
|
150
|
-
|
|
151
|
-
// 根组件
|
|
152
|
-
<ChatProvider provider={provider} storage={AsyncStorage} models={MODELS}>
|
|
153
|
-
<App />
|
|
154
|
-
</ChatProvider>
|
|
155
|
-
|
|
156
|
-
// 子组件
|
|
157
|
-
const { messages, onRequest, sessions, selectedModel } = useChatContext();
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
## 类型定义
|
|
161
|
-
|
|
162
|
-
### ChatMessage
|
|
163
|
-
|
|
164
|
-
```typescript
|
|
165
|
-
interface ChatMessage {
|
|
166
|
-
id: string;
|
|
167
|
-
text: string;
|
|
168
|
-
role: 'user' | 'assistant' | 'system';
|
|
169
|
-
createdAt: Date;
|
|
170
|
-
messageType: 'text' | 'card' | 'system';
|
|
171
|
-
status: 'local' | 'loading' | 'updating' | 'success' | 'error' | 'abort';
|
|
172
|
-
cardType?: string;
|
|
173
|
-
cardData?: { data: Record<string, unknown>; actions: string[] };
|
|
174
|
-
turnId: string;
|
|
175
|
-
extra?: Record<string, unknown>;
|
|
176
|
-
}
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
### MessageInfo
|
|
180
|
-
|
|
181
|
-
```typescript
|
|
182
|
-
interface MessageInfo<Message> {
|
|
183
|
-
id: string;
|
|
184
|
-
message: Message;
|
|
185
|
-
status: MessageStatus;
|
|
186
|
-
extra?: Record<string, unknown>;
|
|
187
|
-
}
|
|
188
|
-
```
|