@opentiny/tiny-robot-kit 0.4.0 → 0.4.1
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 +59 -262
- package/package.json +28 -2
package/README.md
CHANGED
|
@@ -1,289 +1,86 @@
|
|
|
1
1
|
# tiny-robot-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
@opentiny/tiny-robot-kit 是 TinyRobot 提供的数据层工具包,用于统一处理 AI 大模型调用、消息状态与多会话管理。
|
|
4
|
+
它帮助你在任意 UI 上快速实现聊天、流式响应和会话持久化等常见 AI 交互能力。
|
|
4
5
|
|
|
5
|
-
##
|
|
6
|
+
## 功能概览
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
- **消息管理**:`useMessage` 管理消息状态与模型交互流程。
|
|
9
|
+
- **会话管理**:`useConversation` 在 useMessage 之上提供多会话能力。
|
|
10
|
+
- **工具函数(Utils)**:处理 SSE、消息格式与响应解析。
|
|
8
11
|
|
|
9
|
-
|
|
12
|
+
完整 API 请参考文档:
|
|
10
13
|
|
|
11
|
-
|
|
14
|
+
- `useMessage`:<https://docs.opentiny.design/tiny-robot/tools/message>
|
|
15
|
+
- `useConversation`:<https://docs.opentiny.design/tiny-robot/tools/conversation>
|
|
16
|
+
- 工具函数:<https://docs.opentiny.design/tiny-robot/tools/utils>
|
|
12
17
|
|
|
13
|
-
|
|
14
|
-
new AIClient(config: AIModelConfig)
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
#### 方法
|
|
18
|
-
|
|
19
|
-
- `chat(request: ChatCompletionRequest): Promise<ChatCompletionResponse>`
|
|
20
|
-
发送聊天请求并获取响应。
|
|
21
|
-
|
|
22
|
-
- `chatStream(request: ChatCompletionRequest, handler: StreamHandler): Promise<void>`
|
|
23
|
-
发送流式聊天请求并通过处理器处理响应。
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
## 基本用法
|
|
27
|
-
|
|
28
|
-
### 创建客户端并发送消息
|
|
29
|
-
|
|
30
|
-
```typescript
|
|
31
|
-
import { AIClient } from '@opentiny/tiny-robot-kit';
|
|
32
|
-
|
|
33
|
-
// 创建客户端
|
|
34
|
-
const client = new AIClient({
|
|
35
|
-
provider: 'openai',
|
|
36
|
-
apiKey: 'your-api-key',
|
|
37
|
-
defaultModel: 'gpt-3.5-turbo'
|
|
38
|
-
});
|
|
18
|
+
## 安装
|
|
39
19
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
{ role: 'system', content: '你是一个有用的助手。' },
|
|
46
|
-
{ role: 'user', content: '你好,请介绍一下自己。' }
|
|
47
|
-
],
|
|
48
|
-
options: {
|
|
49
|
-
temperature: 0.7
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
console.log(response.choices[0].message.content);
|
|
54
|
-
} catch (error) {
|
|
55
|
-
console.error('聊天出错:', error);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
chat();
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
### 使用流式响应
|
|
63
|
-
|
|
64
|
-
```typescript
|
|
65
|
-
import { AIClient } from '@opentiny/tiny-robot-kit';
|
|
66
|
-
|
|
67
|
-
const client = new AIClient({
|
|
68
|
-
provider: 'openai',
|
|
69
|
-
apiKey: 'your-api-key'
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
async function streamChat() {
|
|
73
|
-
try {
|
|
74
|
-
const controller: AbortController = new AbortController()
|
|
75
|
-
await client.chatStream({
|
|
76
|
-
messages: [
|
|
77
|
-
{ role: 'user', content: '写一个简短的故事。' }
|
|
78
|
-
],
|
|
79
|
-
options: {
|
|
80
|
-
stream: true, // 启用流式响应
|
|
81
|
-
signal: controller.signal // 传递 AbortController 的 signal用于中断请求
|
|
82
|
-
}
|
|
83
|
-
}, {
|
|
84
|
-
onData: (data) => {
|
|
85
|
-
// 处理流式数据
|
|
86
|
-
const content = data.choices[0]?.delta?.content || '';
|
|
87
|
-
process.stdout.write(content);
|
|
88
|
-
},
|
|
89
|
-
onError: (error) => {
|
|
90
|
-
console.error('流式响应错误:', error);
|
|
91
|
-
},
|
|
92
|
-
onDone: () => {
|
|
93
|
-
console.log('\n流式响应完成');
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
// controller.abort() // 中断请求
|
|
97
|
-
} catch (error) {
|
|
98
|
-
console.error('流式聊天出错:', error);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
streamChat();
|
|
20
|
+
```bash
|
|
21
|
+
pnpm add @opentiny/tiny-robot-kit
|
|
22
|
+
# 或
|
|
23
|
+
npm install @opentiny/tiny-robot-kit
|
|
24
|
+
yarn add @opentiny/tiny-robot-kit
|
|
103
25
|
```
|
|
104
26
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
#### 选项
|
|
108
|
-
|
|
109
|
-
`useMessage` 接受以下选项:
|
|
110
|
-
|
|
111
|
-
```typescript
|
|
112
|
-
interface UseMessageOptions {
|
|
113
|
-
/** AI客户端实例 */
|
|
114
|
-
client: AIClient;
|
|
115
|
-
/** 是否默认使用流式响应 */
|
|
116
|
-
useStreamByDefault?: boolean;
|
|
117
|
-
/** 错误消息模板 */
|
|
118
|
-
errorMessage?: string;
|
|
119
|
-
/** 初始消息列表 */
|
|
120
|
-
initialMessages?: ChatMessage[];
|
|
121
|
-
}
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
#### 返回值
|
|
125
|
-
|
|
126
|
-
`useMessage` 返回以下内容:
|
|
127
|
-
```typescript
|
|
128
|
-
interface UseMessageReturn {
|
|
129
|
-
messages: ChatMessage[];
|
|
130
|
-
/** 消息状态 */
|
|
131
|
-
messageState: Reactive<MessageState>;
|
|
132
|
-
/** 输入消息 */
|
|
133
|
-
inputMessage: Ref<string>;
|
|
134
|
-
/** 是否使用流式响应 */
|
|
135
|
-
useStream: Ref<boolean>;
|
|
136
|
-
/** 发送消息 */
|
|
137
|
-
sendMessage: (content?: string, clearInput?: boolean) => Promise<void>
|
|
138
|
-
/** 清空消息 */
|
|
139
|
-
clearMessages: () => void;
|
|
140
|
-
/** 添加消息 */
|
|
141
|
-
addMessage: (message: ChatMessage) => void;
|
|
142
|
-
/** 中止请求 */
|
|
143
|
-
abortRequest: () => void;
|
|
144
|
-
/** 重试请求 */
|
|
145
|
-
retryRequest: () => Promise<void>;
|
|
146
|
-
}
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
#### MessageState 接口
|
|
150
|
-
```typescript
|
|
151
|
-
interface MessageState {
|
|
152
|
-
status: STATUS
|
|
153
|
-
errorMsg: string | null
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
enum STATUS {
|
|
157
|
-
INIT = 'init', // 初始状态
|
|
158
|
-
PROCESSING = 'processing', // AI请求正在处理中, 还未响应,显示加载动画
|
|
159
|
-
STREAMING = 'streaming', // 流式响应中分块数据返回中
|
|
160
|
-
FINISHED = 'finished', // AI请求已完成
|
|
161
|
-
ABORTED = 'aborted', // 用户中止请求
|
|
162
|
-
ERROR = 'error', // AI请求发生错误
|
|
163
|
-
}
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
### useConversation
|
|
167
|
-
|
|
168
|
-
对话管理与数据持久化
|
|
169
|
-
|
|
170
|
-
#### 基本用法
|
|
27
|
+
## 基本用法
|
|
171
28
|
|
|
172
|
-
|
|
173
|
-
import { useConversation, AIClient } from '@opentiny/tiny-robot-kit'
|
|
29
|
+
### useMessage —— 管理 AI 消息
|
|
174
30
|
|
|
175
|
-
|
|
176
|
-
provider: 'openai',
|
|
177
|
-
apiKey: 'your-api-key',
|
|
178
|
-
defaultModel: 'gpt-3.5-turbo'
|
|
179
|
-
});
|
|
31
|
+
`useMessage` 管理消息列表、请求/处理状态、流式响应、插件体系以及工具调用逻辑,而真正的 HTTP 请求由你提供的 `responseProvider` 负责。
|
|
180
32
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
messageManager, // 与 useMessage 返回一致,具体查看useMessage
|
|
184
|
-
createConversation,
|
|
185
|
-
switchConversation,
|
|
186
|
-
deleteConversation,
|
|
187
|
-
// ...
|
|
188
|
-
} = useConversation({ client })
|
|
33
|
+
```ts
|
|
34
|
+
import { useMessage } from '@opentiny/tiny-robot-kit'
|
|
189
35
|
|
|
190
|
-
const
|
|
36
|
+
const message = useMessage({
|
|
37
|
+
// responseProvider 负责调用你的后端 / 模型接口
|
|
38
|
+
async responseProvider(requestBody, abortSignal) {
|
|
39
|
+
const res = await fetch('/api/chat', {
|
|
40
|
+
method: 'POST',
|
|
41
|
+
body: JSON.stringify(requestBody),
|
|
42
|
+
signal: abortSignal,
|
|
43
|
+
headers: { 'Content-Type': 'application/json' },
|
|
44
|
+
})
|
|
191
45
|
|
|
192
|
-
|
|
46
|
+
return await res.json()
|
|
47
|
+
},
|
|
48
|
+
})
|
|
193
49
|
```
|
|
194
50
|
|
|
195
|
-
|
|
51
|
+
更多进阶用法(流式响应、插件、自定义分块(chunk)处理、工具调用等),请查看 <https://docs.opentiny.design/tiny-robot/tools/message>。
|
|
196
52
|
|
|
197
|
-
|
|
53
|
+
### useConversation —— 管理多会话
|
|
198
54
|
|
|
199
|
-
|
|
200
|
-
interface UseConversationReturn {
|
|
201
|
-
/** 会话状态 */
|
|
202
|
-
state: ConversationState;
|
|
203
|
-
/** 消息管理 */
|
|
204
|
-
messageManager: UseMessageReturn;
|
|
205
|
-
/** 创建新会话 */
|
|
206
|
-
createConversation: (title?: string, metadata?: Record<string, any>) => string;
|
|
207
|
-
/** 切换会话 */
|
|
208
|
-
switchConversation: (id: string) => void;
|
|
209
|
-
/** 删除会话 */
|
|
210
|
-
deleteConversation: (id: string) => void;
|
|
211
|
-
/** 更新会话标题 */
|
|
212
|
-
updateTitle: (id: string, title: string) => void;
|
|
213
|
-
/** 更新会话元数据 */
|
|
214
|
-
updateMetadata: (id: string, metadata: Record<string, any>) => void;
|
|
215
|
-
/** 保存会话 */
|
|
216
|
-
saveConversations: () => Promise<void>;
|
|
217
|
-
/** 加载会话 */
|
|
218
|
-
loadConversations: () => Promise<void>;
|
|
219
|
-
/** 生成会话标题 */
|
|
220
|
-
generateTitle: (id: string) => Promise<string>;
|
|
221
|
-
/** 获取当前会话 */
|
|
222
|
-
getCurrentConversation: () => Conversation | null;
|
|
223
|
-
}
|
|
224
|
-
```
|
|
55
|
+
`useConversation` 基于 `useMessage` 之上,提供多会话管理能力,并支持多种存储策略(LocalStorage、IndexedDB、自定义等)。
|
|
225
56
|
|
|
226
|
-
|
|
57
|
+
```ts
|
|
58
|
+
import { useConversation } from '@opentiny/tiny-robot-kit'
|
|
227
59
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
}
|
|
60
|
+
const { conversations, activeConversation, createConversation, switchConversation } = useConversation({
|
|
61
|
+
useMessageOptions: {
|
|
62
|
+
async responseProvider(requestBody, abortSignal) {
|
|
63
|
+
const res = await fetch('/api/chat', {
|
|
64
|
+
method: 'POST',
|
|
65
|
+
body: JSON.stringify(requestBody),
|
|
66
|
+
signal: abortSignal,
|
|
67
|
+
headers: { 'Content-Type': 'application/json' },
|
|
68
|
+
})
|
|
69
|
+
return await res.json()
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
})
|
|
237
73
|
```
|
|
238
74
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
```typescript
|
|
242
|
-
|
|
243
|
-
interface Conversation {
|
|
244
|
-
/** 会话ID */
|
|
245
|
-
id: string;
|
|
246
|
-
/** 会话标题 */
|
|
247
|
-
title: string;
|
|
248
|
-
/** 创建时间 */
|
|
249
|
-
createdAt: number;
|
|
250
|
-
/** 更新时间 */
|
|
251
|
-
updatedAt: number;
|
|
252
|
-
/** 自定义元数据 */
|
|
253
|
-
metadata?: Record<string, any>;
|
|
254
|
-
/** 消息 */
|
|
255
|
-
messages: ChatMessage[];
|
|
256
|
-
}
|
|
257
|
-
```
|
|
75
|
+
例如 `localStorageStrategyFactory` 和 `indexedDBStorageStrategyFactory` 等存储策略的详细用法,请参考 <https://docs.opentiny.design/tiny-robot/tools/conversation>。
|
|
258
76
|
|
|
259
|
-
|
|
77
|
+
### 工具函数 Utils —— 处理 SSE 与响应
|
|
260
78
|
|
|
261
|
-
|
|
79
|
+
Utils 模块提供了一些与 `useMessage` 搭配使用的常用工具函数:
|
|
262
80
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
/** 加载会话列表 */
|
|
268
|
-
loadConversations: () => Promise<Conversation[]> | Conversation[];
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
// 自定义存储策略示例
|
|
272
|
-
class CustomStorageStrategy implements ConversationStorageStrategy {
|
|
273
|
-
async saveConversations(conversations: Conversation[]) {
|
|
274
|
-
// 实现自定义存储逻辑
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
async loadConversations(): Promise<Conversation[]> {
|
|
278
|
-
// 实现自定义加载逻辑
|
|
279
|
-
return [];
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// 使用自定义存储策略
|
|
284
|
-
const conversationManager = useConversation({
|
|
285
|
-
client,
|
|
286
|
-
storage: new CustomStorageStrategy(),
|
|
287
|
-
});
|
|
288
|
-
```
|
|
81
|
+
- `sseStreamToGenerator`:把 SSE `Response` 转换为异步生成器。
|
|
82
|
+
- `formatMessages`:将多种形式的消息统一为 `ChatMessage[]`。
|
|
83
|
+
- `extractTextFromResponse`:从大模型响应中提取纯文本内容。
|
|
84
|
+
- `handleSSEStream`:通过回调方式消费 SSE 流式响应。
|
|
289
85
|
|
|
86
|
+
详细函数签名与行为说明,请查看 <https://docs.opentiny.design/tiny-robot/tools/utils>。
|
package/package.json
CHANGED
|
@@ -1,10 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opentiny/tiny-robot-kit",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
|
+
"homepage": "https://docs.opentiny.design/tiny-robot/",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/opentiny/tiny-robot.git"
|
|
8
|
+
},
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/opentiny/tiny-robot/issues"
|
|
11
|
+
},
|
|
4
12
|
"publishConfig": {
|
|
5
13
|
"access": "public"
|
|
6
14
|
},
|
|
7
15
|
"description": "AI大模型请求与数据处理工具包",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"vue",
|
|
18
|
+
"vue3",
|
|
19
|
+
"ai",
|
|
20
|
+
"ai-client",
|
|
21
|
+
"ai-sdk",
|
|
22
|
+
"chat",
|
|
23
|
+
"chatbot",
|
|
24
|
+
"llm",
|
|
25
|
+
"openai",
|
|
26
|
+
"assistant",
|
|
27
|
+
"streaming",
|
|
28
|
+
"composables",
|
|
29
|
+
"conversation",
|
|
30
|
+
"model-provider",
|
|
31
|
+
"tiny-robot",
|
|
32
|
+
"opentiny"
|
|
33
|
+
],
|
|
8
34
|
"main": "dist/index.js",
|
|
9
35
|
"module": "dist/index.mjs",
|
|
10
36
|
"types": "dist/index.d.ts",
|
|
@@ -29,5 +55,5 @@
|
|
|
29
55
|
"dependencies": {
|
|
30
56
|
"idb": "^8.0.3"
|
|
31
57
|
},
|
|
32
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "e7d831041cd49068110e17c5b5f410ed7fa2706c"
|
|
33
59
|
}
|