@unif/react-native-chat-sdk 0.4.1 → 0.5.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.
Files changed (100) hide show
  1. package/lib/commonjs/hooks/ChatProvider.js +6 -5
  2. package/lib/commonjs/hooks/ChatProvider.js.map +1 -1
  3. package/lib/commonjs/hooks/useXChat.js +3 -11
  4. package/lib/commonjs/hooks/useXChat.js.map +1 -1
  5. package/lib/commonjs/hooks/useXConversations.js +24 -4
  6. package/lib/commonjs/hooks/useXConversations.js.map +1 -1
  7. package/lib/commonjs/hooks/useXModel.js.map +1 -1
  8. package/lib/commonjs/index.js +19 -0
  9. package/lib/commonjs/index.js.map +1 -1
  10. package/lib/commonjs/providers/AbstractChatProvider.js.map +1 -1
  11. package/lib/commonjs/providers/DeepSeekChatProvider.js.map +1 -1
  12. package/lib/commonjs/providers/OpenAIChatProvider.js +1 -1
  13. package/lib/commonjs/providers/OpenAIChatProvider.js.map +1 -1
  14. package/lib/commonjs/stores/chatStore.js +19 -0
  15. package/lib/commonjs/stores/chatStore.js.map +1 -1
  16. package/lib/commonjs/stores/historyStore.js +26 -0
  17. package/lib/commonjs/stores/historyStore.js.map +1 -1
  18. package/lib/commonjs/tools/XRequest.js +1 -1
  19. package/lib/commonjs/tools/XRequest.js.map +1 -1
  20. package/lib/commonjs/tools/XStream.js +0 -1
  21. package/lib/commonjs/tools/XStream.js.map +1 -1
  22. package/lib/module/hooks/ChatProvider.js +6 -5
  23. package/lib/module/hooks/ChatProvider.js.map +1 -1
  24. package/lib/module/hooks/useXChat.js +5 -13
  25. package/lib/module/hooks/useXChat.js.map +1 -1
  26. package/lib/module/hooks/useXConversations.js +24 -4
  27. package/lib/module/hooks/useXConversations.js.map +1 -1
  28. package/lib/module/hooks/useXModel.js.map +1 -1
  29. package/lib/module/index.js +2 -0
  30. package/lib/module/index.js.map +1 -1
  31. package/lib/module/providers/AbstractChatProvider.js.map +1 -1
  32. package/lib/module/providers/DeepSeekChatProvider.js.map +1 -1
  33. package/lib/module/providers/OpenAIChatProvider.js +1 -1
  34. package/lib/module/providers/OpenAIChatProvider.js.map +1 -1
  35. package/lib/module/stores/chatStore.js +16 -0
  36. package/lib/module/stores/chatStore.js.map +1 -1
  37. package/lib/module/stores/historyStore.js +26 -0
  38. package/lib/module/stores/historyStore.js.map +1 -1
  39. package/lib/module/stores/modelStore.js.map +1 -1
  40. package/lib/module/stores/sessionStore.js.map +1 -1
  41. package/lib/module/tools/XRequest.js +1 -1
  42. package/lib/module/tools/XRequest.js.map +1 -1
  43. package/lib/module/tools/XStream.js +0 -1
  44. package/lib/module/tools/XStream.js.map +1 -1
  45. package/lib/typescript/commonjs/hooks/ChatProvider.d.ts +3 -2
  46. package/lib/typescript/commonjs/hooks/ChatProvider.d.ts.map +1 -1
  47. package/lib/typescript/commonjs/hooks/useXChat.d.ts +1 -2
  48. package/lib/typescript/commonjs/hooks/useXChat.d.ts.map +1 -1
  49. package/lib/typescript/commonjs/hooks/useXConversations.d.ts +3 -0
  50. package/lib/typescript/commonjs/hooks/useXConversations.d.ts.map +1 -1
  51. package/lib/typescript/commonjs/index.d.ts +3 -2
  52. package/lib/typescript/commonjs/index.d.ts.map +1 -1
  53. package/lib/typescript/commonjs/providers/AbstractChatProvider.d.ts.map +1 -1
  54. package/lib/typescript/commonjs/providers/DeepSeekChatProvider.d.ts.map +1 -1
  55. package/lib/typescript/commonjs/providers/OpenAIChatProvider.d.ts.map +1 -1
  56. package/lib/typescript/commonjs/stores/chatStore.d.ts +3 -0
  57. package/lib/typescript/commonjs/stores/chatStore.d.ts.map +1 -1
  58. package/lib/typescript/commonjs/stores/historyStore.d.ts +1 -0
  59. package/lib/typescript/commonjs/stores/historyStore.d.ts.map +1 -1
  60. package/lib/typescript/commonjs/tools/SSEParser.d.ts.map +1 -1
  61. package/lib/typescript/commonjs/tools/XStream.d.ts.map +1 -1
  62. package/lib/typescript/commonjs/types/provider.d.ts.map +1 -1
  63. package/lib/typescript/module/hooks/ChatProvider.d.ts +3 -2
  64. package/lib/typescript/module/hooks/ChatProvider.d.ts.map +1 -1
  65. package/lib/typescript/module/hooks/useXChat.d.ts +1 -2
  66. package/lib/typescript/module/hooks/useXChat.d.ts.map +1 -1
  67. package/lib/typescript/module/hooks/useXConversations.d.ts +3 -0
  68. package/lib/typescript/module/hooks/useXConversations.d.ts.map +1 -1
  69. package/lib/typescript/module/index.d.ts +3 -2
  70. package/lib/typescript/module/index.d.ts.map +1 -1
  71. package/lib/typescript/module/providers/AbstractChatProvider.d.ts.map +1 -1
  72. package/lib/typescript/module/providers/DeepSeekChatProvider.d.ts.map +1 -1
  73. package/lib/typescript/module/providers/OpenAIChatProvider.d.ts.map +1 -1
  74. package/lib/typescript/module/stores/chatStore.d.ts +3 -0
  75. package/lib/typescript/module/stores/chatStore.d.ts.map +1 -1
  76. package/lib/typescript/module/stores/historyStore.d.ts +1 -0
  77. package/lib/typescript/module/stores/historyStore.d.ts.map +1 -1
  78. package/lib/typescript/module/tools/SSEParser.d.ts.map +1 -1
  79. package/lib/typescript/module/tools/XStream.d.ts.map +1 -1
  80. package/lib/typescript/module/types/provider.d.ts.map +1 -1
  81. package/package.json +1 -1
  82. package/src/hooks/ChatProvider.tsx +24 -12
  83. package/src/hooks/useXChat.ts +11 -23
  84. package/src/hooks/useXConversations.ts +39 -10
  85. package/src/hooks/useXModel.ts +2 -2
  86. package/src/index.ts +31 -19
  87. package/src/providers/AbstractChatProvider.ts +7 -3
  88. package/src/providers/DeepSeekChatProvider.ts +4 -7
  89. package/src/providers/OpenAIChatProvider.ts +6 -6
  90. package/src/stores/chatStore.ts +29 -10
  91. package/src/stores/historyStore.ts +45 -8
  92. package/src/stores/modelStore.ts +1 -1
  93. package/src/stores/sessionStore.ts +1 -1
  94. package/src/tools/SSEParser.ts +1 -1
  95. package/src/tools/XRequest.ts +1 -1
  96. package/src/tools/XStream.ts +0 -1
  97. package/src/types/provider.ts +2 -2
  98. package/lib/commonjs/index.md +0 -188
  99. package/lib/module/index.md +0 -188
  100. package/src/index.md +0 -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
- ```