@will1123/lx-ui-utils 1.0.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 ADDED
@@ -0,0 +1,234 @@
1
+ # @lx-ui/utils
2
+
3
+ LX UI 通用工具函数库,提供常用的工具函数和 AI 相关功能。
4
+
5
+ ## 特性
6
+
7
+ - 🚀 **TypeScript 支持** - 完整的类型定义
8
+ - 📦 **Tree-Shaking 支持** - 按需引入,减少打包体积
9
+ - 🎯 **零依赖** - 无需安装额外依赖
10
+ - 🔧 **AI 相关工具** - SSE、思考过程提取、Markdown 渲染
11
+
12
+ ## 安装
13
+
14
+ ```bash
15
+ npm install @lx-ui/utils
16
+ # 或
17
+ yarn add @lx-ui/utils
18
+ # 或
19
+ pnpm add @lx-ui/utils
20
+ ```
21
+
22
+ ## 使用方法
23
+
24
+ ### 全量导入
25
+
26
+ ```typescript
27
+ import { sse, extractThinking, renderMarkdown } from '@lx-ui/utils'
28
+ ```
29
+
30
+ ### 按需导入(推荐)
31
+
32
+ ```typescript
33
+ import { sse } from '@lx-ui/utils'
34
+ import { extractThinking } from '@lx-ui/utils'
35
+ import { renderMarkdown } from '@lx-ui/utils'
36
+ ```
37
+
38
+ ## API 文档
39
+
40
+ ### SSE (Server-Sent Events)
41
+
42
+ 封装 SSE 请求,支持流式响应。
43
+
44
+ #### 类型定义
45
+
46
+ ```typescript
47
+ interface SSEConfig {
48
+ url: string // 请求 URL
49
+ method?: 'GET' | 'POST' // 请求方法,默认 GET
50
+ headers?: Record<string, string> // 请求头
51
+ body?: any // 请求体(POST 请求时使用)
52
+ timeout?: number // 请求超时时间(毫秒)
53
+ }
54
+
55
+ interface SSEHandlers {
56
+ onMessage?: (message: SSEMessage) => void
57
+ onOpen?: () => void
58
+ onClose?: () => void
59
+ onError?: (error: Error) => void
60
+ }
61
+ ```
62
+
63
+ #### 使用示例
64
+
65
+ ```typescript
66
+ import { sse } from '@lx-ui/utils'
67
+
68
+ // GET 请求(使用 EventSource)
69
+ const connection = sse(
70
+ {
71
+ url: 'https://api.example.com/stream'
72
+ },
73
+ {
74
+ onMessage: (message) => {
75
+ console.log('收到消息:', message.data)
76
+ },
77
+ onOpen: () => {
78
+ console.log('连接已打开')
79
+ },
80
+ onClose: () => {
81
+ console.log('连接已关闭')
82
+ },
83
+ onError: (error) => {
84
+ console.error('连接错误:', error)
85
+ }
86
+ }
87
+ )
88
+
89
+ // 关闭连接
90
+ connection.close()
91
+
92
+ // POST 请求(使用 fetch)
93
+ const connection2 = sse(
94
+ {
95
+ url: 'https://api.example.com/stream',
96
+ method: 'POST',
97
+ headers: {
98
+ 'Authorization': 'Bearer your-token'
99
+ },
100
+ body: {
101
+ prompt: '你好'
102
+ },
103
+ timeout: 30000
104
+ },
105
+ {
106
+ onMessage: (message) => {
107
+ console.log('收到消息:', message.data)
108
+ }
109
+ }
110
+ )
111
+ ```
112
+
113
+ ### 思考过程提取
114
+
115
+ 从 AI 响应中提取 `<think>` 标签内容。
116
+
117
+ #### 类型定义
118
+
119
+ ```typescript
120
+ interface ThinkingExtractConfig {
121
+ tagName?: string // 思考标签名称,默认 'think'
122
+ includeTags?: boolean // 是否包含标签本身
123
+ removeRest?: boolean // 是否移除剩余内容
124
+ }
125
+
126
+ interface ThinkingResult {
127
+ thinking: string // 提取的思考内容
128
+ rest: string // 剩余内容
129
+ hasThinking: boolean // 是否找到思考内容
130
+ }
131
+ ```
132
+
133
+ #### 使用示例
134
+
135
+ ```typescript
136
+ import { extractThinking, createThinkingExtractor } from '@lx-ui/utils'
137
+
138
+ // 完整模式
139
+ const text = `<think>
140
+ 这是 AI 的思考过程
141
+ </think>
142
+ 这是实际回答内容`
143
+
144
+ const result = extractThinking(text)
145
+ console.log(result.thinking) // "\n这是 AI 的思考过程\n"
146
+ console.log(result.rest) // "这是实际回答内容"
147
+ console.log(result.hasThinking) // true
148
+
149
+ // 流式模式(用于 SSE)
150
+ const extractor = createThinkingExtractor({
151
+ tagName: 'think',
152
+ includeTags: false
153
+ })
154
+
155
+ // 逐步追加文本
156
+ const chunk1 = extractor.append('<think>这是')
157
+ console.log(chunk1.thinking) // "这是"
158
+
159
+ const chunk2 = extractor.append('思考过程')
160
+ console.log(chunk2.thinking) // "这是思考过程"
161
+
162
+ const chunk3 = extractor.append('</think>实际回答')
163
+ console.log(chunk3.thinking) // "这是思考过程"
164
+ console.log(chunk3.isComplete) // true
165
+ ```
166
+
167
+ ### Markdown 渲染
168
+
169
+ 将 Markdown 文本渲染为 HTML。
170
+
171
+ #### 类型定义
172
+
173
+ ```typescript
174
+ interface MarkdownRenderConfig {
175
+ highlight?: boolean // 是否启用代码高亮
176
+ highlightLanguages?: Record<string, string>
177
+ taskList?: boolean // 是否渲染任务列表,默认 true
178
+ table?: boolean // 是否渲染表格,默认 true
179
+ link?: boolean // 是否渲染链接,默认 true
180
+ image?: boolean // 是否渲染图片,默认 true
181
+ }
182
+ ```
183
+
184
+ #### 使用示例
185
+
186
+ ```typescript
187
+ import { renderMarkdown, createMarkdownRenderer } from '@lx-ui/utils'
188
+
189
+ // 完整模式
190
+ const markdown = `# 标题
191
+
192
+ 这是一段**粗体**和*斜体*文字。
193
+
194
+ - [ ] 未完成任务
195
+ - [x] 已完成任务
196
+
197
+ | 列1 | 列2 |
198
+ |-----|-----|
199
+ | A | B |
200
+
201
+ [链接](https://example.com)
202
+ `
203
+
204
+ const html = renderMarkdown(markdown)
205
+ console.log(html)
206
+
207
+ // 流式模式(用于 SSE)
208
+ const renderer = createMarkdownRenderer({
209
+ table: true,
210
+ taskList: true,
211
+ onUpdate: (html) => {
212
+ console.log('更新渲染:', html)
213
+ }
214
+ })
215
+
216
+ // 逐步追加文本
217
+ renderer.append('# 标题\n\n这是一段')
218
+ renderer.append('**粗体**文字。')
219
+ renderer.complete()
220
+ ```
221
+
222
+ ## 构建
223
+
224
+ ```bash
225
+ # 安装依赖
226
+ yarn install
227
+
228
+ # 构建 utils 包
229
+ yarn workspace @lx-ui/utils build
230
+ ```
231
+
232
+ ## 许可证
233
+
234
+ MIT