@h-ai/cli 0.1.0-alpha.10

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 (109) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +167 -0
  3. package/dist/chunk-2H7UOFLK.js +10 -0
  4. package/dist/chunk-2H7UOFLK.js.map +1 -0
  5. package/dist/chunk-LANIDJC7.js +659 -0
  6. package/dist/chunk-LANIDJC7.js.map +1 -0
  7. package/dist/chunk-YV7WPUI6.js +206 -0
  8. package/dist/chunk-YV7WPUI6.js.map +1 -0
  9. package/dist/config-templates-S5RI2MC6.js +5 -0
  10. package/dist/config-templates-S5RI2MC6.js.map +1 -0
  11. package/dist/index.d.ts +2 -0
  12. package/dist/index.js +1650 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/skill-templates-N5DU5FNX.js +5 -0
  15. package/dist/skill-templates-N5DU5FNX.js.map +1 -0
  16. package/dist/src-XJ6YXNAF.js +15030 -0
  17. package/dist/src-XJ6YXNAF.js.map +1 -0
  18. package/package.json +59 -0
  19. package/templates/README.md +126 -0
  20. package/templates/apps/admin/messages/en-US.json +33 -0
  21. package/templates/apps/admin/messages/zh-CN.json +33 -0
  22. package/templates/apps/admin/src/routes/+layout.svelte.hbs +30 -0
  23. package/templates/apps/admin/src/routes/+page.svelte.hbs +35 -0
  24. package/templates/apps/admin/src/routes/admin/+layout.svelte.hbs +44 -0
  25. package/templates/apps/admin/src/routes/admin/+page.svelte.hbs +45 -0
  26. package/templates/apps/admin/src/routes/admin/settings/+page.svelte.hbs +37 -0
  27. package/templates/apps/admin/src/routes/api/health/+server.ts +12 -0
  28. package/templates/apps/android-app/.env.hbs +3 -0
  29. package/templates/apps/android-app/capacitor.config.ts.hbs +12 -0
  30. package/templates/apps/android-app/messages/en-US.json +24 -0
  31. package/templates/apps/android-app/messages/zh-CN.json +24 -0
  32. package/templates/apps/android-app/src/lib/api.ts.hbs +30 -0
  33. package/templates/apps/android-app/src/lib/capacitor.ts +21 -0
  34. package/templates/apps/android-app/src/routes/+layout.svelte.hbs +42 -0
  35. package/templates/apps/android-app/src/routes/+layout.ts +7 -0
  36. package/templates/apps/android-app/src/routes/+page.svelte.hbs +27 -0
  37. package/templates/apps/api/src/routes/+layout.svelte +14 -0
  38. package/templates/apps/api/src/routes/+page.svelte +11 -0
  39. package/templates/apps/api/src/routes/api/v1/health/+server.ts +14 -0
  40. package/templates/apps/api/src/routes/api/v1/items/+server.ts +37 -0
  41. package/templates/apps/api/src/routes/api/v1/items/[id]/+server.ts +30 -0
  42. package/templates/apps/h5/messages/en-US.json +37 -0
  43. package/templates/apps/h5/messages/zh-CN.json +37 -0
  44. package/templates/apps/h5/src/routes/+layout.svelte.hbs +57 -0
  45. package/templates/apps/h5/src/routes/+page.svelte.hbs +27 -0
  46. package/templates/apps/h5/src/routes/discover/+page.svelte.hbs +27 -0
  47. package/templates/apps/h5/src/routes/profile/+page.svelte.hbs +27 -0
  48. package/templates/apps/website/messages/en-US.json +32 -0
  49. package/templates/apps/website/messages/zh-CN.json +32 -0
  50. package/templates/apps/website/src/routes/+layout.svelte.hbs +66 -0
  51. package/templates/apps/website/src/routes/+page.svelte.hbs +33 -0
  52. package/templates/apps/website/src/routes/about/+page.svelte.hbs +27 -0
  53. package/templates/apps/website/src/routes/api/health/+server.ts +12 -0
  54. package/templates/apps/website/src/routes/contact/+page.svelte.hbs +59 -0
  55. package/templates/apps/website/src/routes/services/+page.svelte.hbs +63 -0
  56. package/templates/base/e2e/helpers.ts +49 -0
  57. package/templates/base/package.json.hbs +67 -0
  58. package/templates/base/playwright.config.ts +32 -0
  59. package/templates/base/src/app.css +2 -0
  60. package/templates/base/src/app.d.ts.hbs +18 -0
  61. package/templates/base/src/app.html.hbs +16 -0
  62. package/templates/base/src/hooks.server.ts.hbs +123 -0
  63. package/templates/base/src/lib/server/init.ts.hbs +190 -0
  64. package/templates/base/svelte.config.js.hbs +30 -0
  65. package/templates/base/tsconfig.json +8 -0
  66. package/templates/base/vite.config.ts.hbs +27 -0
  67. package/templates/features/ai/routes/api/chat/+server.ts +26 -0
  68. package/templates/features/datapipe/routes/api/datapipe/pipeline/+server.ts +41 -0
  69. package/templates/features/iam/routes-admin/(auth)/+layout.svelte.hbs +21 -0
  70. package/templates/features/iam/routes-admin/(auth)/auth/login/+page.svelte.hbs +98 -0
  71. package/templates/features/iam/routes-admin/(auth)/auth/register/+page.svelte.hbs +124 -0
  72. package/templates/features/iam/routes-h5/auth/login/+page.svelte.hbs +98 -0
  73. package/templates/features/iam/routes-h5/auth/register/+page.svelte.hbs +114 -0
  74. package/templates/features/iam/routes-shared/api/auth/login/+server.ts +32 -0
  75. package/templates/features/iam/routes-shared/api/auth/logout/+server.ts +15 -0
  76. package/templates/features/iam/routes-shared/api/auth/me/+server.ts +12 -0
  77. package/templates/features/iam/routes-shared/api/auth/register/+server.ts +26 -0
  78. package/templates/features/payment/routes/api/v1/payment/create/+server.ts +30 -0
  79. package/templates/features/payment/routes/api/v1/payment/notify/[provider]/+server.ts +42 -0
  80. package/templates/features/payment/routes/api/v1/payment/query/[orderNo]/+server.ts +25 -0
  81. package/templates/features/storage/routes/api/upload/+server.ts +35 -0
  82. package/templates/features/vecdb/routes/api/vecdb/search/+server.ts +36 -0
  83. package/templates/i18n/project.inlang/settings.json +11 -0
  84. package/templates/skills/AGENTS.md +26 -0
  85. package/templates/skills/CLAUDE.md +12 -0
  86. package/templates/skills/copilot-instructions.md +61 -0
  87. package/templates/skills/hai-ai/SKILL.md +721 -0
  88. package/templates/skills/hai-api-client/SKILL.md +355 -0
  89. package/templates/skills/hai-app-create/SKILL.md +371 -0
  90. package/templates/skills/hai-app-review/SKILL.md +156 -0
  91. package/templates/skills/hai-app-tests/SKILL.md +468 -0
  92. package/templates/skills/hai-audit/SKILL.md +145 -0
  93. package/templates/skills/hai-build/SKILL.md +319 -0
  94. package/templates/skills/hai-cache/SKILL.md +190 -0
  95. package/templates/skills/hai-capacitor/SKILL.md +377 -0
  96. package/templates/skills/hai-core/SKILL.md +484 -0
  97. package/templates/skills/hai-crypto/SKILL.md +198 -0
  98. package/templates/skills/hai-datapipe/SKILL.md +220 -0
  99. package/templates/skills/hai-deploy/SKILL.md +216 -0
  100. package/templates/skills/hai-iam/SKILL.md +361 -0
  101. package/templates/skills/hai-kit/SKILL.md +484 -0
  102. package/templates/skills/hai-payment/SKILL.md +275 -0
  103. package/templates/skills/hai-reach/SKILL.md +315 -0
  104. package/templates/skills/hai-reldb/SKILL.md +465 -0
  105. package/templates/skills/hai-scheduler/SKILL.md +188 -0
  106. package/templates/skills/hai-storage/SKILL.md +276 -0
  107. package/templates/skills/hai-ui/SKILL.md +438 -0
  108. package/templates/skills/hai-vecdb/SKILL.md +279 -0
  109. package/templates/skills/opencode.json +11 -0
@@ -0,0 +1,279 @@
1
+ ---
2
+ name: hai-vecdb
3
+ description: 使用 @h-ai/vecdb 进行向量数据库操作(LanceDB/pgvector/Qdrant)的集合管理与向量增删改查;当需求涉及向量存储、相似度搜索、嵌入检索或语义搜索时使用。
4
+ ---
5
+
6
+ # hai-vecdb
7
+
8
+ > `@h-ai/vecdb` 提供统一的向量数据库操作接口,支持 LanceDB、pgvector、Qdrant,包含集合管理和向量 CRUD/搜索。
9
+
10
+ ---
11
+
12
+ ## 运行环境
13
+
14
+ > ⚠️ **服务端模块(Node.js only)。** 浏览器端不直接操作向量数据库,通过 `@h-ai/ai` 的 API 端点间接使用(如 RAG 检索、知识库查询)。
15
+
16
+ ---
17
+
18
+ ## 适用场景
19
+
20
+ - 新增或修改向量数据库访问逻辑(集合管理/向量插入/搜索)
21
+ - 接入不同向量数据库后端(LanceDB / pgvector / Qdrant)
22
+ - 基于 `HaiVecdbError` 做错误分支处理
23
+ - 构建 RAG、语义搜索、知识库等 AI 场景
24
+
25
+ ---
26
+
27
+ ## 使用步骤
28
+
29
+ ### 1. 配置
30
+
31
+ ```yaml
32
+ # config/_vecdb.yml
33
+
34
+ # LanceDB(默认,嵌入式本地存储)
35
+ type: lancedb
36
+ path: ./data/vecdb
37
+
38
+ # pgvector
39
+ # type: pgvector
40
+ # url: ${HAI_VECDB_PG_URL:postgres://user:pass@localhost:5432/mydb}
41
+ # indexType: hnsw
42
+ # tablePrefix: vec_
43
+
44
+ # Qdrant
45
+ # type: qdrant
46
+ # url: ${HAI_VECDB_QDRANT_URL:http://localhost:6333}
47
+ # apiKey: ${HAI_VECDB_QDRANT_API_KEY:}
48
+ ```
49
+
50
+ ### 2. 初始化与关闭
51
+
52
+ ```typescript
53
+ import { core } from '@h-ai/core'
54
+ import { vecdb } from '@h-ai/vecdb'
55
+
56
+ await vecdb.init(core.config.get('vecdb'))
57
+ // ... 使用向量数据库
58
+ await vecdb.close()
59
+ ```
60
+
61
+ ### 3. 选择操作接口
62
+
63
+ | 接口 | 用途 | 入口 |
64
+ | ---------- | -------------------------- | ----------------- |
65
+ | collection | 集合创建/删除/查询/判断存在 | `vecdb.collection` |
66
+ | vector | 向量插入/更新/删除/搜索/计数 | `vecdb.vector` |
67
+
68
+ ---
69
+
70
+ ## 核心 API
71
+
72
+ ### 集合操作 — `vecdb.collection`
73
+
74
+ | 方法 | 签名 | 说明 |
75
+ | -------- | ------------------------------------------ | -------------- |
76
+ | `create` | `(name, options) => HaiResult<void>` | 创建集合 |
77
+ | `drop` | `(name) => HaiResult<void>` | 删除集合 |
78
+ | `exists` | `(name) => HaiResult<boolean>` | 判断集合是否存在 |
79
+ | `info` | `(name) => HaiResult<CollectionInfo>` | 获取集合信息 |
80
+ | `list` | `() => HaiResult<string[]>` | 列出所有集合 |
81
+
82
+ > 所有方法均返回 `Promise<HaiResult<T>>`,上表省略异步与错误类型。
83
+
84
+ ```typescript
85
+ // 创建集合(指定维度和度量)
86
+ await vecdb.collection.create('docs', { dimension: 1536, metric: 'cosine' })
87
+
88
+ // 查询集合信息
89
+ const info = await vecdb.collection.info('docs')
90
+ if (info.success) {
91
+ // info.data => { name, dimension, metric, count }
92
+ }
93
+
94
+ // 列出所有集合
95
+ const list = await vecdb.collection.list()
96
+ ```
97
+
98
+ **CollectionCreateOptions**:
99
+
100
+ ```typescript
101
+ interface CollectionCreateOptions {
102
+ dimension: number // 向量维度(必填)
103
+ metric?: 'cosine' | 'euclidean' | 'dot' // 距离度量(默认 cosine)
104
+ }
105
+ ```
106
+
107
+ ### 向量操作 — `vecdb.vector`
108
+
109
+ | 方法 | 签名 | 说明 |
110
+ | -------- | --------------------------------------------------- | -------------- |
111
+ | `insert` | `(collection, documents) => HaiResult<void>` | 批量插入 |
112
+ | `upsert` | `(collection, documents) => HaiResult<void>` | 批量更新/插入 |
113
+ | `delete` | `(collection, ids) => HaiResult<void>` | 按 ID 删除 |
114
+ | `search` | `(collection, vector, options?) => HaiResult<SearchResult[]>` | 向量搜索 |
115
+ | `count` | `(collection) => HaiResult<number>` | 文档计数 |
116
+
117
+ > 所有方法均返回 `Promise<HaiResult<T>>`,上表省略异步与错误类型。
118
+
119
+ ```typescript
120
+ // 插入向量文档
121
+ await vecdb.vector.insert('docs', [
122
+ { id: 'doc-1', vector: embedding, content: '文档内容', metadata: { source: 'wiki' } },
123
+ { id: 'doc-2', vector: embedding2, content: '另一段内容' },
124
+ ])
125
+
126
+ // 向量搜索(返回相似度排序结果)
127
+ const result = await vecdb.vector.search('docs', queryVector, {
128
+ topK: 10,
129
+ minScore: 0.7,
130
+ filter: { source: 'wiki' },
131
+ })
132
+ if (result.success) {
133
+ for (const item of result.data) {
134
+ // item => { id, score, content, metadata }
135
+ }
136
+ }
137
+
138
+ // 更新已有文档(按 id 匹配)
139
+ await vecdb.vector.upsert('docs', [
140
+ { id: 'doc-1', vector: newEmbedding, content: '更新后的内容' },
141
+ ])
142
+
143
+ // 删除
144
+ await vecdb.vector.delete('docs', ['doc-1', 'doc-2'])
145
+ ```
146
+
147
+ **VectorDocument**:
148
+
149
+ ```typescript
150
+ interface VectorDocument {
151
+ id: string // 文档唯一标识
152
+ vector: number[] // 向量数据
153
+ content?: string // 文本内容(可选)
154
+ metadata?: Record<string, unknown> // 元数据(可选)
155
+ }
156
+ ```
157
+
158
+ **VectorSearchOptions**:
159
+
160
+ ```typescript
161
+ interface VectorSearchOptions {
162
+ topK?: number // 返回数量(默认 10)
163
+ filter?: Record<string, unknown> // 元数据过滤(键值精确匹配)
164
+ minScore?: number // 最低相似度阈值(0-1)
165
+ }
166
+ ```
167
+
168
+ ---
169
+
170
+ ## 错误码 — `HaiVecdbError`
171
+
172
+ | 错误码 | code | 说明 |
173
+ | ----------------------------------------- | ----------------- | ------------------ |
174
+ | `HaiVecdbError.CONNECTION_FAILED` | `hai:vecdb:001` | 连接失败 |
175
+ | `HaiVecdbError.QUERY_FAILED` | `hai:vecdb:002` | 查询失败 |
176
+ | `HaiVecdbError.COLLECTION_NOT_FOUND` | `hai:vecdb:003` | 集合不存在 |
177
+ | `HaiVecdbError.COLLECTION_ALREADY_EXISTS`| `hai:vecdb:004` | 集合已存在 |
178
+ | `HaiVecdbError.DIMENSION_MISMATCH` | `hai:vecdb:005` | 向量维度不匹配 |
179
+ | `HaiVecdbError.INSERT_FAILED` | `hai:vecdb:006` | 插入失败 |
180
+ | `HaiVecdbError.DELETE_FAILED` | `hai:vecdb:007` | 删除失败 |
181
+ | `HaiVecdbError.UPDATE_FAILED` | `hai:vecdb:008` | 更新失败 |
182
+ | `HaiVecdbError.INDEX_BUILD_FAILED` | `hai:vecdb:009` | 索引构建失败 |
183
+ | `HaiVecdbError.NOT_INITIALIZED` | `hai:vecdb:010` | 未初始化 |
184
+ | `HaiVecdbError.CONFIG_ERROR` | `hai:vecdb:011` | 配置错误 |
185
+ | `HaiVecdbError.UNSUPPORTED_TYPE` | `hai:vecdb:012` | 不支持的数据库类型 |
186
+ | `HaiVecdbError.DRIVER_NOT_FOUND` | `hai:vecdb:013` | 驱动未安装 |
187
+ | `HaiVecdbError.SERIALIZATION_FAILED` | `hai:vecdb:014` | 序列化失败 |
188
+
189
+ ---
190
+
191
+ ## 常见模式
192
+
193
+ ### RAG 检索
194
+
195
+ ```typescript
196
+ import { ai } from '@h-ai/ai'
197
+ import { vecdb } from '@h-ai/vecdb'
198
+
199
+ // 将查询文本转为向量
200
+ const embedResult = await ai.embedding.embedText(query)
201
+ if (!embedResult.success) return embedResult
202
+
203
+ // 检索最相似的文档
204
+ const searchResult = await vecdb.vector.search('knowledge', embedResult.data, {
205
+ topK: 5,
206
+ minScore: 0.7,
207
+ })
208
+ if (!searchResult.success) return searchResult
209
+
210
+ // 拼接上下文,送入 LLM
211
+ const context = searchResult.data.map(r => r.content).join('\n\n')
212
+ const chatResult = await ai.llm.chat({
213
+ messages: [
214
+ { role: 'system', content: `参考以下资料回答用户问题:\n${context}` },
215
+ { role: 'user', content: query },
216
+ ],
217
+ })
218
+ ```
219
+
220
+ ### 文档入库
221
+
222
+ ```typescript
223
+ import { datapipe } from '@h-ai/datapipe'
224
+ import { ai } from '@h-ai/ai'
225
+ import { vecdb } from '@h-ai/vecdb'
226
+
227
+ // 清洗 + 分块
228
+ const pipeline = await datapipe.pipeline()
229
+ .clean({ removeHtml: true })
230
+ .chunk({ mode: 'markdown', maxSize: 1000, overlap: 100 })
231
+ .run(rawText)
232
+
233
+ if (!pipeline.success) return pipeline
234
+
235
+ // 批量生成嵌入
236
+ const embeddings = await ai.embedding.embedBatch(
237
+ pipeline.data.chunks.map(c => c.content),
238
+ )
239
+ if (!embeddings.success) return embeddings
240
+
241
+ // 批量入库
242
+ const docs = pipeline.data.chunks.map((chunk, i) => ({
243
+ id: `doc-${i}`,
244
+ vector: embeddings.data[i],
245
+ content: chunk.content,
246
+ metadata: chunk.metadata,
247
+ }))
248
+ await vecdb.vector.insert('knowledge', docs)
249
+ ```
250
+
251
+ ### 错误分支处理
252
+
253
+ ```typescript
254
+ import { vecdb, HaiVecdbError } from '@h-ai/vecdb'
255
+
256
+ const result = await vecdb.collection.create('docs', { dimension: 1536 })
257
+ if (!result.success) {
258
+ switch (result.error.code) {
259
+ case HaiVecdbError.COLLECTION_ALREADY_EXISTS.code:
260
+ // 集合已存在,可跳过
261
+ break
262
+ case HaiVecdbError.NOT_INITIALIZED.code:
263
+ // 未初始化,需先 vecdb.init()
264
+ break
265
+ default:
266
+ // 意外错误
267
+ break
268
+ }
269
+ }
270
+ ```
271
+
272
+ ---
273
+
274
+ ## 相关 Skills
275
+
276
+ - `hai-ai`:LLM 与 Embedding 能力
277
+ - `hai-datapipe`:文本清洗与分块
278
+ - `hai-reldb`:关系型数据库(知识库需要同时使用 reldb 存储结构化数据)
279
+ - `hai-core`:配置管理、HaiResult 模型
@@ -0,0 +1,11 @@
1
+ {
2
+ "$schema": "https://opencode.ai/config.json",
3
+ "skills": {
4
+ "paths": [
5
+ ".agents/skills"
6
+ ]
7
+ },
8
+ "instructions": [
9
+ "AGENTS.md"
10
+ ]
11
+ }