@h-ai/cli 0.1.0-alpha.65 → 0.1.0-alpha.67

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 (141) hide show
  1. package/README.en.md +66 -251
  2. package/README.ja.md +67 -252
  3. package/README.md +66 -251
  4. package/dist/{chunk-47V5OT22.js → chunk-7DSURTJ4.js} +32 -115
  5. package/dist/chunk-7DSURTJ4.js.map +1 -0
  6. package/dist/{chunk-IPG3QNYK.js → chunk-AOQ4N657.js} +212 -8
  7. package/dist/chunk-AOQ4N657.js.map +1 -0
  8. package/dist/cli-config-templates-6AHNUGBN.js +5 -0
  9. package/dist/{cli-config-templates-SJ2MBIR7.js.map → cli-config-templates-6AHNUGBN.js.map} +1 -1
  10. package/dist/{cli-skill-templates-A5VIR5J6.js → cli-skill-templates-FC3QRKDO.js} +3 -3
  11. package/dist/{cli-skill-templates-A5VIR5J6.js.map → cli-skill-templates-FC3QRKDO.js.map} +1 -1
  12. package/dist/index.js +644 -391
  13. package/dist/index.js.map +1 -1
  14. package/dist/{src-6PPBKQ6Q.js → src-2V6VP4I5.js} +972 -520
  15. package/dist/src-2V6VP4I5.js.map +1 -0
  16. package/package.json +8 -8
  17. package/templates/README.md +48 -145
  18. package/templates/apps/admin/src/routes/admin/+layout.svelte.hbs +1 -1
  19. package/templates/apps/api/.dockerignore.hbs +18 -12
  20. package/templates/apps/api/.gitignore.hbs +9 -1
  21. package/templates/apps/api/apps/{{projectName}}-contract/README.md.hbs +23 -2
  22. package/templates/apps/api/apps/{{projectName}}-service/README.md.hbs +28 -7
  23. package/templates/apps/api/apps/{{projectName}}-service/package.json.hbs +1 -3
  24. package/templates/apps/api/hai.build.json.hbs +1 -0
  25. package/templates/apps/api/images/app.Dockerfile.hbs +35 -0
  26. package/templates/apps/api/package.json.hbs +10 -8
  27. package/templates/apps/fullstack/.dockerignore.hbs +18 -14
  28. package/templates/apps/fullstack/.gitignore.hbs +9 -1
  29. package/templates/apps/fullstack/apps/{{projectName}}-app/README.md.hbs +23 -8
  30. package/templates/apps/fullstack/apps/{{projectName}}-app/index.html.hbs +1 -0
  31. package/templates/apps/fullstack/apps/{{projectName}}-app/package.json.hbs +0 -2
  32. package/templates/apps/fullstack/apps/{{projectName}}-app/public/runtime-config.js.hbs +4 -0
  33. package/templates/apps/fullstack/apps/{{projectName}}-app/vite.config.ts.hbs +3 -3
  34. package/templates/apps/fullstack/apps/{{projectName}}-desktop/README.md.hbs +25 -10
  35. package/templates/apps/fullstack/apps/{{projectName}}-desktop/package.json.hbs +2 -3
  36. package/templates/apps/fullstack/apps/{{projectName}}-desktop/src-tauri/src/lib.rs.hbs +73 -2
  37. package/templates/apps/fullstack/apps/{{projectName}}-desktop/src-tauri/tauri.conf.json.hbs +2 -2
  38. package/templates/apps/fullstack/apps/{{projectName}}-desktop/vite.config.ts.hbs +2 -2
  39. package/templates/apps/fullstack/apps/{{projectName}}-miniapp/README.md.hbs +16 -6
  40. package/templates/apps/fullstack/apps/{{projectName}}-web/README.md.hbs +24 -10
  41. package/templates/apps/fullstack/apps/{{projectName}}-web/package.json.hbs +0 -2
  42. package/templates/apps/fullstack/apps/{{projectName}}-web/vite.config.ts.hbs +3 -3
  43. package/templates/apps/fullstack/e2e/fullstack.spec.ts.hbs +12 -1
  44. package/templates/apps/fullstack/hai.build.json.hbs +7 -0
  45. package/templates/apps/fullstack/images/app.Dockerfile.hbs +36 -0
  46. package/templates/apps/fullstack/images/web.Dockerfile.hbs +34 -0
  47. package/templates/apps/fullstack/package.json.hbs +7 -12
  48. package/templates/apps/fullstack/packages/{{projectName}}-contract/README.md.hbs +23 -2
  49. package/templates/apps/fullstack/packages/{{projectName}}-serv/README.md.hbs +28 -7
  50. package/templates/apps/fullstack/packages/{{projectName}}-serv/package.json.hbs +0 -1
  51. package/templates/apps/fullstack/packages/{{projectName}}-shared/.gitignore.hbs +7 -0
  52. package/templates/apps/fullstack/packages/{{projectName}}-shared/README.md.hbs +33 -12
  53. package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/components/AppShell.svelte.hbs +2 -2
  54. package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/pages/settings.svelte.hbs +3 -3
  55. package/templates/apps/fullstack/packages/{{projectName}}-shared/tests/api-client.test.ts.hbs +2 -2
  56. package/templates/apps/h5/src/routes/+layout.svelte.hbs +2 -2
  57. package/templates/apps/mobile-app/index.html.hbs +1 -0
  58. package/templates/apps/mobile-app/public/runtime-config.js +1 -0
  59. package/templates/apps/mobile-app/src/App.svelte.hbs +2 -2
  60. package/templates/apps/mobile-app/src/lib/api.ts.hbs +2 -1
  61. package/templates/apps/mobile-app/src/lib/app-config.ts.hbs +3 -2
  62. package/templates/apps/mobile-app/src/lib/pages/home.svelte.hbs +2 -2
  63. package/templates/apps/website/src/routes/+layout.svelte.hbs +3 -3
  64. package/templates/apps/website/src/routes/services/+page.svelte.hbs +1 -1
  65. package/templates/base/.dockerignore.hbs +18 -13
  66. package/templates/base/.gitignore.hbs +9 -1
  67. package/templates/base/e2e/app.spec.ts.hbs +10 -0
  68. package/templates/base/hai.build.json.hbs +1 -0
  69. package/templates/base/images/app.Dockerfile.hbs +64 -0
  70. package/templates/base/{docker → images/web}/nginx.conf.hbs +6 -0
  71. package/templates/base/package.json.hbs +20 -19
  72. package/templates/base/svelte.config.js.hbs +2 -0
  73. package/templates/base/vite.config.ts.hbs +9 -3
  74. package/templates/shared/.gitattributes +11 -0
  75. package/templates/shared/container-runtime-config.sh +30 -0
  76. package/templates/shared/run-e2e.mjs +65 -0
  77. package/templates/shared/vite-runtime-config.ts +54 -0
  78. package/templates/skills/AGENTS.md +3 -3
  79. package/templates/skills/bridges/admin/AGENTS.md +1 -1
  80. package/templates/skills/bridges/api/AGENTS.md +1 -1
  81. package/templates/skills/bridges/fullstack/AGENTS.md.hbs +1 -1
  82. package/templates/skills/bridges/h5/AGENTS.md +1 -1
  83. package/templates/skills/bridges/mobile-app/AGENTS.md +1 -1
  84. package/templates/skills/bridges/website/AGENTS.md +1 -1
  85. package/templates/skills/hai-api-client/SKILL.md +14 -161
  86. package/templates/skills/hai-api-client/reference.md +166 -0
  87. package/templates/skills/hai-api-contract/SKILL.md +15 -273
  88. package/templates/skills/hai-api-contract/reference.md +278 -0
  89. package/templates/skills/hai-app-create/SKILL.md +2 -2
  90. package/templates/skills/hai-app-create/development.md +6 -4
  91. package/templates/skills/hai-app-create/reference.md +5 -4
  92. package/templates/skills/hai-app-review/SKILL.md +1 -1
  93. package/templates/skills/hai-app-tests/SKILL.md +4 -3
  94. package/templates/skills/hai-app-tests/reference.md +2 -2
  95. package/templates/skills/hai-app-tests/validation.md +31 -0
  96. package/templates/skills/hai-audit/SKILL.md +17 -142
  97. package/templates/skills/hai-audit/reference.md +145 -0
  98. package/templates/skills/hai-build/SKILL.md +24 -3
  99. package/templates/skills/hai-build/reference.md +10 -9
  100. package/templates/skills/hai-cache/SKILL.md +15 -182
  101. package/templates/skills/hai-cache/reference.md +187 -0
  102. package/templates/skills/hai-capacitor/SKILL.md +4 -3
  103. package/templates/skills/hai-capacitor/reference.md +6 -4
  104. package/templates/skills/hai-ci/SKILL.md +2 -0
  105. package/templates/skills/hai-core/SKILL.md +3 -3
  106. package/templates/skills/hai-crypto/SKILL.md +16 -252
  107. package/templates/skills/hai-crypto/reference.md +256 -0
  108. package/templates/skills/hai-datapipe/SKILL.md +15 -196
  109. package/templates/skills/hai-datapipe/reference.md +202 -0
  110. package/templates/skills/hai-deploy/SKILL.md +28 -271
  111. package/templates/skills/hai-deploy/middleware.md +6 -18
  112. package/templates/skills/hai-framework-sync/SKILL.md +2 -2
  113. package/templates/skills/hai-framework-sync/sync.md +1 -1
  114. package/templates/skills/hai-iam/SKILL.md +1 -3
  115. package/templates/skills/hai-kit/SKILL.md +12 -167
  116. package/templates/skills/hai-kit/reference.md +172 -0
  117. package/templates/skills/hai-payment/SKILL.md +19 -251
  118. package/templates/skills/hai-payment/reference.md +256 -0
  119. package/templates/skills/hai-pr-review/SKILL.md +4 -4
  120. package/templates/skills/hai-reach/SKILL.md +2 -2
  121. package/templates/skills/hai-reldb/SKILL.md +1 -1
  122. package/templates/skills/hai-scheduler/SKILL.md +15 -195
  123. package/templates/skills/hai-scheduler/reference.md +202 -0
  124. package/templates/skills/hai-serv/SKILL.md +2 -2
  125. package/templates/skills/hai-spec-implement/SKILL.md +1 -1
  126. package/templates/skills/hai-spec-implement/workflow.md +3 -3
  127. package/templates/skills/hai-storage/SKILL.md +17 -238
  128. package/templates/skills/hai-storage/reference.md +243 -0
  129. package/templates/skills/hai-ui/SKILL.md +1 -1
  130. package/templates/skills/hai-ui/reference.md +11 -5
  131. package/templates/skills/hai-vecdb/SKILL.md +2 -2
  132. package/dist/chunk-47V5OT22.js.map +0 -1
  133. package/dist/chunk-IPG3QNYK.js.map +0 -1
  134. package/dist/cli-config-templates-SJ2MBIR7.js +0 -5
  135. package/dist/src-6PPBKQ6Q.js.map +0 -1
  136. package/templates/apps/api/Dockerfile.hbs +0 -48
  137. package/templates/apps/fullstack/Dockerfile.hbs +0 -69
  138. package/templates/base/Dockerfile.hbs +0 -79
  139. /package/templates/apps/fullstack/{docker → images/web}/19-runtime-config.envsh.hbs +0 -0
  140. /package/templates/apps/fullstack/{docker → images/web}/nginx.conf.hbs +0 -0
  141. /package/templates/apps/fullstack/{docker → images/web}/runtime-config.js.template.hbs +0 -0
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: hai-datapipe
3
- description: "使用 @h-ai/datapipe 清洗文本、分块并组合 RAG 入库管线。"
3
+ description: "使用 @h-ai/datapipe 清洗和分块文本、组合预处理管线;用于 RAG 入库前处理,不负责 embedding。"
4
4
  ---
5
5
 
6
6
  # hai-datapipe
@@ -15,202 +15,21 @@ description: "使用 @h-ai/datapipe 清洗文本、分块并组合 RAG 入库管
15
15
  | 输出 | 清洗/分块的 HaiResult;管线工厂按签名使用 |
16
16
  | 限制 | 纯函数,无 init/close;区分字符数和模型 token 数,分块结果不等于已生成 embedding。 |
17
17
 
18
- > `@h-ai/datapipe` 提供文本清洗(clean)、多模式分块(chunk)和可组合管线(pipeline),纯函数模块,无需初始化。
18
+ ## 使用路径
19
19
 
20
- ## 运行环境
20
+ 1. 直接使用 clean/chunk/pipeline,无 init/close;清洗或分块失败后不读 data。
21
+ 2. 根据原文结构选择模式,明确 maxSize、overlap 与字符/词边界;这里不负责模型 token 预算。
22
+ 3. 自定义正则仅来自可信配置。入库前先检查管线结果,再生成 embedding,并验证向量维度与集合匹配。
23
+ 4. 验证空文本、非法配置、边界切分与自定义转换失败;保留文档内容及必要元数据。
21
24
 
22
- > **服务端模块(Node.js only)。** 通常在 AI 知识库入库或文档预处理场景中使用,由 `@h-ai/ai` 内部调用或在服务端显式调用。
25
+ ## 按需参考
23
26
 
24
- ## 适用场景
27
+ 按本次操作读取对应小节;以已安装版本的公开类型核验签名,示例中的业务变量由应用提供。
25
28
 
26
- - 文本清洗:移除 HTML 标签、URL、Email,标准化空白
27
- - 文本分块:句子、段落、Markdown 标题、字数、字符、自定义分隔符
28
- - RAG 入库前的文档预处理管线
29
- - 管线模式组合多步清洗 + 分块 + 自定义转换
30
-
31
- ## 使用步骤
32
-
33
- ### 直接调用(无需 init)
34
-
35
- ```typescript
36
- import { datapipe } from '@h-ai/datapipe'
37
-
38
- // 清洗
39
- const cleaned = datapipe.clean(htmlText, { removeHtml: true, removeUrls: true })
40
-
41
- // 分块
42
- const chunks = datapipe.chunk(cleaned.data, { mode: 'markdown', maxSize: 2000, overlap: 200 })
43
- ```
44
-
45
- ### 管线模式
46
-
47
- ```typescript
48
- const result = await datapipe.pipeline()
49
- .clean({ removeHtml: true, removeUrls: true })
50
- .transform(text => text.toLowerCase())
51
- .chunk({ mode: 'paragraph', maxSize: 1000, overlap: 100 })
52
- .chunkTransform(chunks => chunks.filter(c => c.content.length > 50))
53
- .run(rawText)
54
-
55
- if (result.success) {
56
- // result.data => { text: string, chunks: DataChunk[] }
57
- }
58
- ```
59
-
60
- ## 核心 API
61
-
62
- ### 清洗 — `datapipe.clean`
63
-
64
- ```text
65
- datapipe.clean(text: string, options?: CleanOptionsInput): HaiResult<string>
66
- ```
67
-
68
- **CleanOptionsInput**:
69
-
70
- | 字段 | 类型 | 默认 | 说明 |
71
- | -------------------- | ------------------ | ----- | ------------------------ |
72
- | `removeHtml` | `boolean` | true | 移除 HTML 标签 |
73
- | `removeUrls` | `boolean` | false | 移除 URL |
74
- | `removeEmails` | `boolean` | false | 移除 Email 地址 |
75
- | `normalizeWhitespace`| `boolean` | true | 多空格→单空格、去多余空行 |
76
- | `trim` | `boolean` | true | 去除首尾空白 |
77
- | `customReplacements` | `{ pattern, replacement }[]` | — | 自定义正则替换规则 |
78
-
79
- ### 分块 — `datapipe.chunk`
80
-
81
- ```text
82
- datapipe.chunk(text: string, options: ChunkOptionsInput): HaiResult<DataChunk[]>
83
- ```
84
-
85
- **ChunkOptionsInput**:
86
-
87
- | 字段 | 类型 | 默认 | 说明 |
88
- | ------------------ | --------- | ---- | ------------------------------ |
89
- | `mode` | `ChunkMode` | — | 分块模式(必填) |
90
- | `maxSize` | `number` | 1000 | 分块最大大小 |
91
- | `overlap` | `number` | 0 | 重叠大小(上下文衔接) |
92
- | `separator` | `string` | — | 自定义分隔符正则(mode=custom)|
93
- | `markdownMinLevel` | `number` | 2 | Markdown 最低标题级别(1-6) |
94
- | `markdownKeepTitle`| `boolean` | true | 分块中保留 Markdown 标题 |
95
-
96
- **分块模式**:
97
-
98
- | 模式 | 说明 |
99
- | ----------- | --------------------------- |
100
- | `sentence` | 按句子(中英文句号/问号/感叹号) |
101
- | `paragraph` | 按段落(双换行) |
102
- | `markdown` | 按 Markdown 标题层级 |
103
- | `page` | 按分页符(\f) |
104
- | `word` | 按字数 |
105
- | `character` | 按字符数 |
106
- | `custom` | 自定义正则分隔符 |
107
-
108
- **DataChunk**:
109
-
110
- ```typescript
111
- interface DataChunk {
112
- index: number // 分块索引(从 0 开始)
113
- content: string // 分块内容
114
- metadata?: Record<string, unknown> // 元数据(Markdown 模式含 title、level)
115
- }
116
- ```
117
-
118
- ### 管线 — `datapipe.pipeline()`
119
-
120
- ```typescript
121
- datapipe.pipeline()
122
- .clean(options) // 添加清洗步骤
123
- .transform(fn) // 添加文本转换步骤(同步/异步)
124
- .chunk(options) // 添加分块步骤
125
- .chunkTransform(fn) // 添加分块后处理步骤
126
- .run(text) // 执行管线 → Promise<HaiResult<PipelineResult>>
127
- ```
128
-
129
- **PipelineResult**:
130
-
131
- ```typescript
132
- interface PipelineResult {
133
- text: string // 处理后的文本(分块前的最终文本)
134
- chunks: DataChunk[] // 分块列表(如果管线包含 chunk 步骤)
135
- }
136
- ```
137
-
138
- ## 错误码 — `HaiDatapipeError`
139
-
140
- | 错误码 | code | 说明 |
141
- |--------|------|------|
142
- | `HaiDatapipeError.CLEAN_FAILED` | `hai:datapipe:001` | 清洗失败 |
143
- | `HaiDatapipeError.CHUNK_FAILED` | `hai:datapipe:002` | 分块失败 |
144
- | `HaiDatapipeError.TRANSFORM_FAILED` | `hai:datapipe:003` | 转换失败 |
145
- | `HaiDatapipeError.PIPELINE_FAILED` | `hai:datapipe:004` | 管线执行失败 |
146
- | `HaiDatapipeError.CONFIG_ERROR` | `hai:datapipe:005` | 配置错误 |
147
- | `HaiDatapipeError.MISSING_SEPARATOR` | `hai:datapipe:006` | 自定义分隔符缺失 |
148
-
149
- ## 常见模式
150
-
151
- ### RAG 文档入库前处理
152
-
153
- ```typescript
154
- import { datapipe } from '@h-ai/datapipe'
155
- import { ai } from '@h-ai/ai'
156
- import { vecdb } from '@h-ai/vecdb'
157
-
158
- // 清洗 + Markdown 分块
159
- const result = await datapipe.pipeline()
160
- .clean({ removeHtml: true, removeUrls: true })
161
- .chunk({ mode: 'markdown', maxSize: 1000, overlap: 100 })
162
- .run(rawDocument)
163
-
164
- if (!result.success) return result
165
-
166
- // 生成嵌入
167
- const embeddings = await ai.embedding.embedBatch(
168
- result.data.chunks.map(c => c.content),
169
- )
170
- if (!embeddings.success) return embeddings
171
-
172
- // 入库
173
- const docs = result.data.chunks.map((chunk, i) => ({
174
- id: `chunk-${i}`,
175
- vector: embeddings.data[i],
176
- content: chunk.content,
177
- metadata: chunk.metadata,
178
- }))
179
- await vecdb.vector.insert('knowledge', docs)
180
- ```
181
-
182
- ### 多步清洗管线
183
-
184
- ```typescript
185
- const result = await datapipe.pipeline()
186
- .clean({ removeHtml: true, removeEmails: true })
187
- .transform(text => text.replace(/\[.*?\]/g, '')) // 移除 [注释]
188
- .transform(text => text.replace(/\(https?:.*?\)/g, '')) // 移除 Markdown 链接 URL
189
- .chunk({ mode: 'paragraph', maxSize: 800 })
190
- .chunkTransform(chunks => chunks.filter(c => c.content.length > 20)) // 过滤过短分块
191
- .run(rawText)
192
- ```
193
-
194
- ### 错误分支处理
195
-
196
- ```typescript
197
- import { datapipe, HaiDatapipeError } from '@h-ai/datapipe'
198
-
199
- const result = datapipe.chunk(text, { mode: 'custom' })
200
- if (!result.success) {
201
- switch (result.error.code) {
202
- case HaiDatapipeError.MISSING_SEPARATOR.code:
203
- // mode='custom' 时需提供 separator
204
- break
205
- case HaiDatapipeError.CONFIG_ERROR.code:
206
- // 配置参数校验失败
207
- break
208
- }
209
- }
210
- ```
211
-
212
- ## 相关 Skills
213
-
214
- - `hai-vecdb`:向量数据库存储(分块后入库)
215
- - `hai-ai`:LLM 与 Embedding 能力
216
- - `hai-core`:HaiResult 模型
29
+ - [运行环境](reference.md#运行环境)
30
+ - [适用场景](reference.md#适用场景)
31
+ - [使用步骤](reference.md#使用步骤)
32
+ - [核心 API](reference.md#核心-api)
33
+ - [错误码 — `HaiDatapipeError`](reference.md#错误码--haidatapipeerror)
34
+ - [常见模式](reference.md#常见模式)
35
+ - [相关 Skills](reference.md#相关-skills)
@@ -0,0 +1,202 @@
1
+ # hai-datapipe 参考
2
+
3
+ > `@h-ai/datapipe` 提供文本清洗(clean)、多模式分块(chunk)和可组合管线(pipeline),纯函数模块,无需初始化。
4
+
5
+ ## 运行环境
6
+
7
+ > **服务端模块(Node.js only)。** 通常在 AI 知识库入库或文档预处理场景中使用,由 `@h-ai/ai` 内部调用或在服务端显式调用。
8
+
9
+ ## 适用场景
10
+
11
+ - 文本清洗:移除 HTML 标签、URL、Email,标准化空白
12
+ - 文本分块:句子、段落、Markdown 标题、字数、字符、自定义分隔符
13
+ - RAG 入库前的文档预处理管线
14
+ - 管线模式组合多步清洗 + 分块 + 自定义转换
15
+
16
+ ## 使用步骤
17
+
18
+ ### 直接调用(无需 init)
19
+
20
+ ```typescript
21
+ import { datapipe } from '@h-ai/datapipe'
22
+
23
+ // 清洗
24
+ const cleaned = datapipe.clean(htmlText, { removeHtml: true, removeUrls: true })
25
+
26
+ // 分块
27
+ if (!cleaned.success) return cleaned
28
+ const chunks = datapipe.chunk(cleaned.data, { mode: 'markdown', maxSize: 2000, overlap: 200 })
29
+ ```
30
+
31
+ ### 管线模式
32
+
33
+ ```typescript
34
+ const result = await datapipe.pipeline()
35
+ .clean({ removeHtml: true, removeUrls: true })
36
+ .transform(text => text.toLowerCase())
37
+ .chunk({ mode: 'paragraph', maxSize: 1000, overlap: 100 })
38
+ .chunkTransform(chunks => chunks.filter(c => c.content.length > 50))
39
+ .run(rawText)
40
+
41
+ if (result.success) {
42
+ // result.data => { text: string, chunks: DataChunk[] }
43
+ }
44
+ ```
45
+
46
+ ## 核心 API
47
+
48
+ ### 清洗 — `datapipe.clean`
49
+
50
+ ```text
51
+ datapipe.clean(text: string, options?: CleanOptionsInput): HaiResult<string>
52
+ ```
53
+
54
+ **CleanOptionsInput**:
55
+
56
+ | 字段 | 类型 | 默认 | 说明 |
57
+ | -------------------- | ------------------ | ----- | ------------------------ |
58
+ | `removeHtml` | `boolean` | true | 移除 HTML 标签 |
59
+ | `removeUrls` | `boolean` | false | 移除 URL |
60
+ | `removeEmails` | `boolean` | false | 移除 Email 地址 |
61
+ | `normalizeWhitespace`| `boolean` | true | 多空格→单空格、去多余空行 |
62
+ | `trim` | `boolean` | true | 去除首尾空白 |
63
+ | `customReplacements` | `{ pattern, replacement }[]` | — | 自定义正则替换规则 |
64
+
65
+ ### 分块 — `datapipe.chunk`
66
+
67
+ ```text
68
+ datapipe.chunk(text: string, options: ChunkOptionsInput): HaiResult<DataChunk[]>
69
+ ```
70
+
71
+ **ChunkOptionsInput**:
72
+
73
+ | 字段 | 类型 | 默认 | 说明 |
74
+ | ------------------ | --------- | ---- | ------------------------------ |
75
+ | `mode` | `ChunkMode` | — | 分块模式(必填) |
76
+ | `maxSize` | `number` | 1000 | 分块最大大小 |
77
+ | `overlap` | `number` | 0 | 重叠大小(上下文衔接) |
78
+ | `separator` | `string` | — | 自定义分隔符正则(mode=custom)|
79
+ | `markdownMinLevel` | `number` | 2 | Markdown 最低标题级别(1-6) |
80
+ | `markdownKeepTitle`| `boolean` | true | 分块中保留 Markdown 标题 |
81
+
82
+ **分块模式**:
83
+
84
+ | 模式 | 说明 |
85
+ | ----------- | --------------------------- |
86
+ | `sentence` | 按句子(中英文句号/问号/感叹号) |
87
+ | `paragraph` | 按段落(双换行) |
88
+ | `markdown` | 按 Markdown 标题层级 |
89
+ | `page` | 按分页符(\f) |
90
+ | `word` | 按字数 |
91
+ | `character` | 按字符数 |
92
+ | `custom` | 自定义正则分隔符 |
93
+
94
+ **DataChunk**:
95
+
96
+ ```typescript
97
+ interface DataChunk {
98
+ index: number // 分块索引(从 0 开始)
99
+ content: string // 分块内容
100
+ metadata?: Record<string, unknown> // 元数据(Markdown 模式含 title、level)
101
+ }
102
+ ```
103
+
104
+ ### 管线 — `datapipe.pipeline()`
105
+
106
+ ```typescript
107
+ datapipe.pipeline()
108
+ .clean(options) // 添加清洗步骤
109
+ .transform(fn) // 添加文本转换步骤(同步/异步)
110
+ .chunk(options) // 添加分块步骤
111
+ .chunkTransform(fn) // 添加分块后处理步骤
112
+ .run(text) // 执行管线 → Promise<HaiResult<PipelineResult>>
113
+ ```
114
+
115
+ **PipelineResult**:
116
+
117
+ ```typescript
118
+ interface PipelineResult {
119
+ text: string // 处理后的文本(分块前的最终文本)
120
+ chunks: DataChunk[] // 分块列表(如果管线包含 chunk 步骤)
121
+ }
122
+ ```
123
+
124
+ ## 错误码 — `HaiDatapipeError`
125
+
126
+ | 错误码 | code | 说明 |
127
+ |--------|------|------|
128
+ | `HaiDatapipeError.CLEAN_FAILED` | `hai:datapipe:001` | 清洗失败 |
129
+ | `HaiDatapipeError.CHUNK_FAILED` | `hai:datapipe:002` | 分块失败 |
130
+ | `HaiDatapipeError.TRANSFORM_FAILED` | `hai:datapipe:003` | 转换失败 |
131
+ | `HaiDatapipeError.PIPELINE_FAILED` | `hai:datapipe:004` | 管线执行失败 |
132
+ | `HaiDatapipeError.CONFIG_ERROR` | `hai:datapipe:005` | 配置错误 |
133
+ | `HaiDatapipeError.MISSING_SEPARATOR` | `hai:datapipe:006` | 自定义分隔符缺失 |
134
+
135
+ ## 常见模式
136
+
137
+ ### RAG 文档入库前处理
138
+
139
+ ```typescript
140
+ import { datapipe } from '@h-ai/datapipe'
141
+ import { ai } from '@h-ai/ai'
142
+ import { vecdb } from '@h-ai/vecdb'
143
+
144
+ // 清洗 + Markdown 分块
145
+ const result = await datapipe.pipeline()
146
+ .clean({ removeHtml: true, removeUrls: true })
147
+ .chunk({ mode: 'markdown', maxSize: 1000, overlap: 100 })
148
+ .run(rawDocument)
149
+
150
+ if (!result.success) return result
151
+
152
+ // 生成嵌入
153
+ const embeddings = await ai.embedding.embedBatch(
154
+ result.data.chunks.map(c => c.content),
155
+ )
156
+ if (!embeddings.success) return embeddings
157
+
158
+ // 入库
159
+ const docs = result.data.chunks.map((chunk, i) => ({
160
+ id: `chunk-${i}`,
161
+ vector: embeddings.data[i],
162
+ content: chunk.content,
163
+ metadata: chunk.metadata,
164
+ }))
165
+ await vecdb.vector.insert('knowledge', docs)
166
+ ```
167
+
168
+ ### 多步清洗管线
169
+
170
+ ```typescript
171
+ const result = await datapipe.pipeline()
172
+ .clean({ removeHtml: true, removeEmails: true })
173
+ .transform(text => text.replace(/\[.*?\]/g, '')) // 移除 [注释]
174
+ .transform(text => text.replace(/\(https?:.*?\)/g, '')) // 移除 Markdown 链接 URL
175
+ .chunk({ mode: 'paragraph', maxSize: 800 })
176
+ .chunkTransform(chunks => chunks.filter(c => c.content.length > 20)) // 过滤过短分块
177
+ .run(rawText)
178
+ ```
179
+
180
+ ### 错误分支处理
181
+
182
+ ```typescript
183
+ import { datapipe, HaiDatapipeError } from '@h-ai/datapipe'
184
+
185
+ const result = datapipe.chunk(text, { mode: 'custom' })
186
+ if (!result.success) {
187
+ switch (result.error.code) {
188
+ case HaiDatapipeError.MISSING_SEPARATOR.code:
189
+ // mode='custom' 时需提供 separator
190
+ break
191
+ case HaiDatapipeError.CONFIG_ERROR.code:
192
+ // 配置参数校验失败
193
+ break
194
+ }
195
+ }
196
+ ```
197
+
198
+ ## 相关 Skills
199
+
200
+ - `hai-vecdb`:向量数据库存储(先生成 embedding,再入库)
201
+ - `hai-ai`:LLM 与 Embedding 能力
202
+ - `hai-core`:HaiResult 模型