@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.
Files changed (70) hide show
  1. package/lib/commonjs/hooks/useXConversations.js.map +1 -1
  2. package/lib/commonjs/hooks/useXModel.js.map +1 -1
  3. package/lib/commonjs/index.js.map +1 -1
  4. package/lib/commonjs/providers/AbstractChatProvider.js.map +1 -1
  5. package/lib/commonjs/providers/DeepSeekChatProvider.js.map +1 -1
  6. package/lib/commonjs/providers/OpenAIChatProvider.js +1 -1
  7. package/lib/commonjs/providers/OpenAIChatProvider.js.map +1 -1
  8. package/lib/commonjs/stores/chatStore.js.map +1 -1
  9. package/lib/commonjs/stores/historyStore.js.map +1 -1
  10. package/lib/commonjs/tools/XRequest.js +1 -1
  11. package/lib/commonjs/tools/XRequest.js.map +1 -1
  12. package/lib/commonjs/tools/XStream.js +0 -1
  13. package/lib/commonjs/tools/XStream.js.map +1 -1
  14. package/lib/module/hooks/useXConversations.js.map +1 -1
  15. package/lib/module/hooks/useXModel.js.map +1 -1
  16. package/lib/module/index.js.map +1 -1
  17. package/lib/module/providers/AbstractChatProvider.js.map +1 -1
  18. package/lib/module/providers/DeepSeekChatProvider.js.map +1 -1
  19. package/lib/module/providers/OpenAIChatProvider.js +1 -1
  20. package/lib/module/providers/OpenAIChatProvider.js.map +1 -1
  21. package/lib/module/stores/chatStore.js.map +1 -1
  22. package/lib/module/stores/historyStore.js.map +1 -1
  23. package/lib/module/stores/modelStore.js.map +1 -1
  24. package/lib/module/stores/sessionStore.js.map +1 -1
  25. package/lib/module/tools/XRequest.js +1 -1
  26. package/lib/module/tools/XRequest.js.map +1 -1
  27. package/lib/module/tools/XStream.js +0 -1
  28. package/lib/module/tools/XStream.js.map +1 -1
  29. package/lib/typescript/commonjs/hooks/useXConversations.d.ts.map +1 -1
  30. package/lib/typescript/commonjs/index.d.ts +2 -2
  31. package/lib/typescript/commonjs/index.d.ts.map +1 -1
  32. package/lib/typescript/commonjs/providers/AbstractChatProvider.d.ts.map +1 -1
  33. package/lib/typescript/commonjs/providers/DeepSeekChatProvider.d.ts.map +1 -1
  34. package/lib/typescript/commonjs/providers/OpenAIChatProvider.d.ts.map +1 -1
  35. package/lib/typescript/commonjs/stores/chatStore.d.ts.map +1 -1
  36. package/lib/typescript/commonjs/stores/historyStore.d.ts.map +1 -1
  37. package/lib/typescript/commonjs/tools/SSEParser.d.ts.map +1 -1
  38. package/lib/typescript/commonjs/tools/XStream.d.ts.map +1 -1
  39. package/lib/typescript/commonjs/types/provider.d.ts.map +1 -1
  40. package/lib/typescript/module/hooks/useXConversations.d.ts.map +1 -1
  41. package/lib/typescript/module/index.d.ts +2 -2
  42. package/lib/typescript/module/index.d.ts.map +1 -1
  43. package/lib/typescript/module/providers/AbstractChatProvider.d.ts.map +1 -1
  44. package/lib/typescript/module/providers/DeepSeekChatProvider.d.ts.map +1 -1
  45. package/lib/typescript/module/providers/OpenAIChatProvider.d.ts.map +1 -1
  46. package/lib/typescript/module/stores/chatStore.d.ts.map +1 -1
  47. package/lib/typescript/module/stores/historyStore.d.ts.map +1 -1
  48. package/lib/typescript/module/tools/SSEParser.d.ts.map +1 -1
  49. package/lib/typescript/module/tools/XStream.d.ts.map +1 -1
  50. package/lib/typescript/module/types/provider.d.ts.map +1 -1
  51. package/package.json +1 -1
  52. package/src/hooks/ChatProvider.tsx +3 -3
  53. package/src/hooks/useXChat.ts +3 -3
  54. package/src/hooks/useXConversations.ts +6 -6
  55. package/src/hooks/useXModel.ts +2 -2
  56. package/src/index.ts +26 -19
  57. package/src/providers/AbstractChatProvider.ts +7 -3
  58. package/src/providers/DeepSeekChatProvider.ts +4 -7
  59. package/src/providers/OpenAIChatProvider.ts +6 -6
  60. package/src/stores/chatStore.ts +9 -10
  61. package/src/stores/historyStore.ts +4 -8
  62. package/src/stores/modelStore.ts +1 -1
  63. package/src/stores/sessionStore.ts +1 -1
  64. package/src/tools/SSEParser.ts +1 -1
  65. package/src/tools/XRequest.ts +1 -1
  66. package/src/tools/XStream.ts +0 -1
  67. package/src/types/provider.ts +2 -2
  68. package/lib/commonjs/index.md +0 -188
  69. package/lib/module/index.md +0 -188
  70. package/src/index.md +0 -188
@@ -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
- ```
package/src/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
- ```