@h-ai/cli 0.1.0-alpha5

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