@mybricks/plugin-ai 0.0.1 → 0.0.2

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 (93) hide show
  1. package/package.json +7 -2
  2. package/src/agents/app.ts +188 -60
  3. package/src/agents/common.ts +428 -68
  4. package/src/agents/custom.ts +14 -0
  5. package/src/agents/index.ts +31 -1
  6. package/src/agents/knowledges/README.md +614 -0
  7. package/src/agents/knowledges/SUMMARY.md +527 -0
  8. package/src/agents/knowledges/index.ts +8 -0
  9. package/src/agents/knowledges/knowledge-base.ts +565 -0
  10. package/src/agents/knowledges/knowledge-node.ts +266 -0
  11. package/src/agents/knowledges/types.ts +208 -0
  12. package/src/agents/utils/config.ts +427 -0
  13. package/src/agents/workspace/coding-manager.ts +31 -0
  14. package/src/agents/workspace/components-manager.ts +124 -0
  15. package/src/agents/workspace/outline-focus.ts +188 -0
  16. package/src/agents/workspace/outline-info.ts +520 -0
  17. package/src/agents/workspace/page-tree-generator.ts +83 -0
  18. package/src/agents/workspace/workspace.ts +319 -0
  19. package/src/agents/workspace-by-knowledges/MIGRATION.md +568 -0
  20. package/src/agents/workspace-by-knowledges/README.md +521 -0
  21. package/src/agents/workspace-by-knowledges/index.ts +11 -0
  22. package/src/agents/workspace-by-knowledges/providers/component-docs-provider.ts +92 -0
  23. package/src/agents/workspace-by-knowledges/providers/focus-info-provider.ts +131 -0
  24. package/src/agents/workspace-by-knowledges/providers/index.ts +8 -0
  25. package/src/agents/workspace-by-knowledges/providers/project-info-provider.ts +151 -0
  26. package/src/agents/workspace-by-knowledges/test.ts +240 -0
  27. package/src/agents/workspace-by-knowledges/types.ts +56 -0
  28. package/src/agents/workspace-by-knowledges/utils/components-manager.ts +145 -0
  29. package/src/agents/workspace-by-knowledges/utils/index.ts +8 -0
  30. package/src/agents/workspace-by-knowledges/utils/outline-focus.ts +178 -0
  31. package/src/agents/workspace-by-knowledges/utils/outline-info.ts +521 -0
  32. package/src/agents/workspace-by-knowledges/workspace.ts +166 -0
  33. package/src/api/cloud-components.ts +129 -0
  34. package/src/api-record-replay/README.md +187 -0
  35. package/src/api-record-replay/index.ts +11 -0
  36. package/src/api-record-replay/manager.ts +168 -0
  37. package/src/api-record-replay/recorder.ts +117 -0
  38. package/src/api-record-replay/replayer.ts +148 -0
  39. package/src/components/attachments/index.less +117 -0
  40. package/src/components/attachments/index.tsx +136 -0
  41. package/src/components/icons/index.tsx +21 -1
  42. package/src/components/index.less +34 -0
  43. package/src/components/mention/index.less +23 -0
  44. package/src/components/mention/index.tsx +19 -0
  45. package/src/components/messages/index.less +444 -237
  46. package/src/components/messages/index.tsx +371 -88
  47. package/src/components/sender/index.less +203 -0
  48. package/src/components/sender/index.tsx +298 -0
  49. package/src/components/types.ts +31 -0
  50. package/src/constants/index.ts +8 -0
  51. package/src/context/RequestStatusTracker.ts +50 -0
  52. package/src/context/index.ts +68 -6
  53. package/src/{types.d.ts → global.d.ts} +40 -5
  54. package/src/index.tsx +212 -32
  55. package/src/preset/agents.ts +380 -0
  56. package/src/preset/createTemplates.ts +25 -0
  57. package/src/preset/index.ts +12 -0
  58. package/src/preset/prompts.ts +235 -0
  59. package/src/preset/requestAsStream.ts +246 -0
  60. package/src/preset/user.ts +6 -0
  61. package/src/startView/components/header/header.less +17 -0
  62. package/src/startView/components/header/header.tsx +15 -0
  63. package/src/startView/components/index.ts +1 -0
  64. package/src/startView/index.less +22 -204
  65. package/src/startView/index.tsx +35 -203
  66. package/src/tools/analyze-and-expand-prd.ts +192 -86
  67. package/src/tools/analyze-requirement-and-components.ts +589 -0
  68. package/src/tools/answer.ts +59 -0
  69. package/src/tools/build-process.ts +1174 -0
  70. package/src/tools/coding-subagent-as-tool.ts +119 -0
  71. package/src/tools/generate-ui-content.ts +1083 -0
  72. package/src/tools/index.ts +22 -19
  73. package/src/tools/open-dsl.ts +69 -0
  74. package/src/tools/refactor-ui-content.ts +801 -0
  75. package/src/tools/utils.ts +880 -28
  76. package/src/types/index.ts +4 -0
  77. package/src/view/components/header/header.less +36 -2
  78. package/src/view/components/header/header.tsx +47 -2
  79. package/src/view/components/index.ts +0 -2
  80. package/src/view/index.tsx +158 -8
  81. package/src/tools/answer-user.ts +0 -35
  82. package/src/tools/focus-element.ts +0 -47
  83. package/src/tools/generate-page.ts +0 -750
  84. package/src/tools/get-component-info-by-ids.ts +0 -166
  85. package/src/tools/get-component-info.ts +0 -53
  86. package/src/tools/get-components-doc-and-prd.ts +0 -137
  87. package/src/tools/get-focus-mybricks-dsl.ts +0 -26
  88. package/src/tools/get-mybricks-dsl.ts +0 -73
  89. package/src/tools/modify-component.ts +0 -385
  90. package/src/view/components/messages/messages.less +0 -228
  91. package/src/view/components/messages/messages.tsx +0 -172
  92. package/src/view/components/sender/sender.less +0 -44
  93. package/src/view/components/sender/sender.tsx +0 -62
@@ -0,0 +1,521 @@
1
+ # Workspace by Knowledges
2
+
3
+ 基于知识库架构重构的工作空间实现。
4
+
5
+ ## 🎉 新功能亮点
6
+
7
+ ### 1. Provider 改用 `new` 而不是 `extends`
8
+ - ✅ 更好的代码提示和类型检查
9
+ - ✅ 更简洁,不需要理解继承
10
+ - ✅ 使用 `new DirectoryProvider(config)` 创建 provider
11
+
12
+ ### 2. 动态文档支持多目录
13
+ - ✅ 在 `KnowledgeBase` 底层直接支持动态文档
14
+ - ✅ 支持注册多个动态文档目录
15
+ - ✅ 每个目录可独立配置名称、描述、权重、是否隐藏
16
+ - ✅ 动态文档可以聚合在不同的目录下
17
+
18
+ **快速使用:**
19
+ ```typescript
20
+ const workspace = new Workspace();
21
+
22
+ // 注册动态文档目录
23
+ workspace.registerDynamicDirectory({
24
+ id: 'opened-pages',
25
+ name: '已打开的页面',
26
+ weight: 80,
27
+ hidden: false
28
+ });
29
+
30
+ // 添加动态文档
31
+ await workspace.openDocument('page1', {
32
+ type: '画布',
33
+ content: '<页面内容>',
34
+ title: '页面1',
35
+ directoryId: 'opened-pages'
36
+ });
37
+ ```
38
+
39
+ 📖 详细文档:[动态文档功能使用指南](./DYNAMIC_DOCS_GUIDE.md)
40
+ 💡 使用示例:[example-usage.ts](./example-usage.ts)
41
+
42
+ ## 概述
43
+
44
+ 将原有的 `workspace` 模块重构为基于 `knowledges` 架构的实现,提供更清晰的文件系统视图和更灵活的扩展能力。从 `context` 获取所有依赖,无需手动传入 API。
45
+
46
+ ## 核心特性
47
+
48
+ - **继承自 KnowledgeBase** - 拥有知识库的所有功能
49
+ - **从 context 获取依赖** - 无需手动传入 API 和 focusInfo
50
+ - **三种 Provider** - 项目信息、聚焦信息、组件配置文档
51
+ - **默认自动打开** - 项目信息和聚焦信息默认打开
52
+ - **动态文档支持** - 可以运行时添加/删除页面和组件文档
53
+ - **权重排序** - Provider 按权重排序,控制显示顺序
54
+ - **隐藏节点** - 组件配置文档是隐藏的,不在目录树中显示但可以打开
55
+
56
+ ## 目录结构
57
+
58
+ ```
59
+ workspace-by-knowledges/
60
+ ├── types.ts # 类型定义
61
+ ├── workspace.ts # Workspace 主类
62
+ ├── providers/ # Provider 实现
63
+ │ ├── project-info-provider.ts # 项目信息 provider(文件形式)
64
+ │ ├── focus-info-provider.ts # 聚焦信息 provider(文件形式)
65
+ │ ├── component-docs-provider.ts # 组件配置文档 provider(隐藏目录)
66
+ │ └── index.ts
67
+ ├── utils/ # 工具函数
68
+ │ ├── components-manager.ts # 组件管理器
69
+ │ ├── outline-focus.ts # 聚焦层级结构生成
70
+ │ └── index.ts
71
+ ├── test.ts # 测试文件
72
+ ├── index.ts # 主导出
73
+ ├── README.md # 文档
74
+ └── MIGRATION.md # 迁移指南
75
+ ```
76
+
77
+ ## 核心目录结构
78
+
79
+ ### 1. 项目信息(project-info)
80
+
81
+ 提供项目的画布索引信息,作为单个文件呈现(默认打开)。
82
+
83
+ **文件结构:**
84
+ ```
85
+ 项目信息.md [默认打开]
86
+ ```
87
+
88
+ **文件内容:**
89
+ ```markdown
90
+ ## 画布索引
91
+ - 首页[id=u_abc123]
92
+ - 用户列表[id=u_def456] 【当前聚焦】
93
+ - 详情页[id=u_ghi789]
94
+ - 详情子页面[id=u_jkl012]
95
+ ```
96
+
97
+ **特点:**
98
+ - 单个 `.md` 文件,位于根目录
99
+ - 默认自动打开
100
+ - 包含所有画布的树形结构
101
+ - 标记当前聚焦的画布
102
+ - 权重:100(最高,排在最前面)
103
+
104
+ ### 2. 聚焦信息(focus-info)
105
+
106
+ 提供当前聚焦元素的层级结构和描述,作为单个文件呈现(默认打开)。
107
+
108
+ **文件结构:**
109
+ ```
110
+ 聚焦信息.md [默认打开]
111
+ ```
112
+
113
+ **文件内容:**
114
+ ```markdown
115
+ ## 聚焦信息
116
+ 以下是当前聚焦组件的简略树结构,展示了聚焦元素的父级、兄弟、子级元素的关系。
117
+ 注意:此树结构并不完整,折叠了无关元素信息,如需详细信息请打开DSL文档获取。
118
+
119
+ <聚焦层级结构>
120
+
121
+ <聚焦描述>
122
+ ```
123
+
124
+ **特点:**
125
+ - 单个 `.md` 文件,位于根目录
126
+ - 默认自动打开
127
+ - 包含聚焦元素的简略树结构
128
+ - 包含聚焦元素的详细描述
129
+ - 权重:90(排在项目信息之后)
130
+
131
+ ### 3. 组件配置文档(component-docs)- 隐藏目录
132
+
133
+ 提供所有组件的详细配置文档,作为隐藏的目录(不在目录树中显示)。
134
+
135
+ **目录结构:**
136
+ ```
137
+ 组件配置文档/ (隐藏)
138
+ ├── mybricks.normal-pc.antd5.button.md (隐藏)
139
+ ├── mybricks.normal-pc.antd5.input.md (隐藏)
140
+ ├── mybricks.normal-pc.antd5.table.md (隐藏)
141
+ └── ... (更多组件)
142
+ ```
143
+
144
+ **特点:**
145
+ - 隐藏目录,不在目录树中显示
146
+ - 目录下的文件也都是隐藏的
147
+ - 文件可以被打开查看
148
+ - 自动处理组件依赖关系(打开组件时自动打开依赖)
149
+ - 包含组件的所有使用说明
150
+ - 权重:0(默认权重)
151
+
152
+ ### 4. 动态文档目录(可选)
153
+
154
+ 可以注册自定义的动态文档目录,用于存放运行时生成的文档:
155
+
156
+ **目录结构:**
157
+ ```
158
+ 已打开的页面/ (可选)
159
+ ├── 首页.md
160
+ ├── 详情页.md
161
+ └── ...
162
+
163
+ 已打开的组件/ (可选)
164
+ ├── 按钮组件.md
165
+ ├── 输入框组件.md
166
+ └── ...
167
+ ```
168
+
169
+ **特点:**
170
+ - 运行时动态添加/删除
171
+ - 可以配置是否隐藏
172
+ - 可以配置权重和显示顺序
173
+
174
+ ## 使用方法
175
+
176
+ ### 基础使用
177
+
178
+ ```typescript
179
+ import { Workspace } from './workspace-by-knowledges';
180
+
181
+ // 创建 Workspace 实例(从 context 获取所有依赖)
182
+ const workspace = new Workspace({
183
+ name: '我的工作空间',
184
+ description: '项目工作空间'
185
+ });
186
+
187
+ // 等待默认文件打开完成
188
+ await workspace.waitForReady();
189
+
190
+ // 获取目录树(隐藏的节点不会显示)
191
+ const tree = await workspace.getDirectoryTree({ showDescription: true });
192
+ console.log(tree);
193
+ // 输出:
194
+ // - 项目信息.md [已打开] (包含项目的画布索引)
195
+ // - 聚焦信息.md [已打开] (当前聚焦组件的树结构和描述)
196
+ // (组件配置文档目录被隐藏,不显示)
197
+
198
+ // 查看已打开的文件
199
+ console.log(workspace.getOpenedFilePaths());
200
+ // ['项目信息.md', '聚焦信息.md']
201
+
202
+ // 导出工作空间内容(用于发送给 AI)
203
+ const content = await workspace.export({ includeTree: true });
204
+ ```
205
+
206
+ ### 打开组件文档
207
+
208
+ ```typescript
209
+ // 方式1:通过路径打开(需要知道完整路径)
210
+ await workspace.openComponentDoc('组件配置文档/mybricks.normal-pc.antd5.button.md');
211
+
212
+ // 方式2:通过 namespace 打开(推荐)
213
+ await workspace.openComponentDocByNamespace('mybricks.normal-pc.antd5.button');
214
+
215
+ // 自动打开依赖组件
216
+ // 如果 button 组件依赖 icon 组件,icon 组件也会被自动打开
217
+ ```
218
+
219
+ ### 动态文档功能
220
+
221
+ ```typescript
222
+ // 1. 注册动态文档目录
223
+ workspace.registerDynamicDirectory({
224
+ id: 'opened-pages',
225
+ name: '已打开的页面',
226
+ description: '用户打开的页面文档',
227
+ weight: 80,
228
+ hidden: false // 不隐藏,在目录树中显示
229
+ });
230
+
231
+ // 2. 打开文档(使用 openDocument 方法)
232
+ await workspace.openDocument('page_u_abc123', {
233
+ type: '画布',
234
+ content: '<页面内容>',
235
+ title: '首页',
236
+ description: '项目首页',
237
+ directoryId: 'opened-pages'
238
+ });
239
+
240
+ // 3. 关闭文档
241
+ workspace.closeDocument('page_u_abc123', 'opened-pages');
242
+
243
+ // 4. 清空所有动态文档
244
+ workspace.clearDynamicDocuments('opened-pages');
245
+ ```
246
+
247
+ ### 高级功能
248
+
249
+ ```typescript
250
+ // 刷新工作空间(当 context.currentFocus 变化时)
251
+ await workspace.refresh();
252
+
253
+ // 查找节点(包括隐藏的节点)
254
+ const node = await workspace.findNode('组件配置文档/mybricks.normal-pc.antd5.button.md');
255
+
256
+ // 获取已打开的文件列表(包括隐藏文件)
257
+ const openedFiles = workspace.getOpenedFilePaths();
258
+
259
+ // 关闭文件
260
+ workspace.closeFile('项目信息.md');
261
+
262
+ // 清空所有已打开的文件
263
+ workspace.clearOpenedFiles();
264
+
265
+ // 获取根节点
266
+ const root = workspace.getRoot();
267
+
268
+ // 获取动态文档
269
+ const doc = workspace.getDynamicDocument('page1', 'opened-pages');
270
+ ```
271
+
272
+ ## 与原 Workspace 的对比
273
+
274
+ ### 原 Workspace
275
+
276
+ ```typescript
277
+ // 需要手动传入 API 和 outlineInfoManager
278
+ const workspace = new WorkSpace(
279
+ { currentFocus },
280
+ workspaceAPI,
281
+ outlineInfoManager
282
+ );
283
+
284
+ // 打开文档
285
+ workspace.openDocument(id);
286
+
287
+ // 获取项目结构
288
+ const projectStruct = workspace.getProjectStruct();
289
+
290
+ // 打开组件文档
291
+ workspace.openComponentDoc(namespace);
292
+
293
+ // 检查是否有组件文档
294
+ if (workspace.hasComponentsDocs()) {
295
+ const docs = workspace.getComponentsDocs();
296
+ }
297
+ ```
298
+
299
+ **特点:**
300
+ - 需要手动传入多个依赖
301
+ - 命令式 API
302
+ - 文档管理较为简单
303
+ - 结构相对扁平
304
+
305
+ ### 新 Workspace
306
+
307
+ ```typescript
308
+ // 从 context 获取所有依赖,无需手动传入
309
+ const workspace = new Workspace({
310
+ name: 'Workspace',
311
+ description: '工作空间知识库'
312
+ });
313
+
314
+ // 默认自动打开项目信息和聚焦信息
315
+ await workspace.waitForReady();
316
+
317
+ // 打开组件文档(通过 namespace)
318
+ await workspace.openComponentDocByNamespace('mybricks.normal-pc.antd5.button');
319
+
320
+ // 打开动态文档
321
+ await workspace.openDocument('page1', {
322
+ type: '画布',
323
+ content: '<页面内容>',
324
+ directoryId: 'opened-pages'
325
+ });
326
+
327
+ // 获取目录树(隐藏节点不显示)
328
+ const tree = await workspace.getDirectoryTree();
329
+
330
+ // 导出所有内容(包含已打开的隐藏文件)
331
+ const content = await workspace.export();
332
+ ```
333
+
334
+ **特点:**
335
+ - 从 context 获取依赖,更简洁
336
+ - 继承自 KnowledgeBase,拥有知识库的所有功能
337
+ - 文件系统视图,更直观
338
+ - 动态内容获取,始终最新
339
+ - 自动处理组件依赖关系
340
+ - 支持动态文档、权重排序、隐藏节点
341
+ - 更好的扩展性
342
+
343
+ ## 优势
344
+
345
+ 1. **从 context 获取依赖**
346
+ - 无需手动传入 API 和 focusInfo
347
+ - 代码更简洁,减少参数传递
348
+
349
+ 2. **清晰的文件系统视图**
350
+ - 所有资源以文件形式呈现
351
+ - 路径清晰,易于理解
352
+
353
+ 3. **动态内容获取**
354
+ - 每次打开文件都获取最新内容
355
+ - 不缓存过期数据
356
+
357
+ 4. **灵活的扩展能力**
358
+ - 继承自 KnowledgeBase,拥有知识库的所有功能
359
+ - 基于 Provider 模式
360
+ - 可以轻松添加新的目录类型
361
+
362
+ 5. **自动依赖管理**
363
+ - 打开组件时自动加载依赖
364
+ - 避免遗漏必要信息
365
+
366
+ 6. **统一的导出接口**
367
+ - 可直接导出为大模型消息
368
+ - 格式统一,易于处理
369
+
370
+ 7. **支持高级功能**
371
+ - 动态文档
372
+ - 权重排序
373
+ - 隐藏节点
374
+ - 自定义根节点
375
+
376
+ ## 快速切换
377
+
378
+ 由于是独立的文件夹,可以方便地在新旧实现之间切换:
379
+
380
+ ```typescript
381
+ // 使用旧实现
382
+ import { WorkSpace } from './workspace/workspace';
383
+
384
+ // 使用新实现
385
+ import { Workspace } from './workspace-by-knowledges';
386
+ ```
387
+
388
+ 两者可以共存,不会相互影响,方便逐步迁移或对比测试。
389
+
390
+ ## 扩展指南
391
+
392
+ ### 添加新的 Provider
393
+
394
+ 如需添加新的目录类型,只需:
395
+
396
+ 1. 在 `providers/` 目录创建新的 provider 文件
397
+ 2. 使用 `new DirectoryProvider(config)` 创建 Provider
398
+ 3. 在 `Workspace` 的构造函数中注册该 Provider
399
+
400
+ 示例:
401
+
402
+ ```typescript
403
+ // providers/my-custom-provider.ts
404
+ import { DirectoryProvider, IKnowledgeNode, KnowledgeNodeType } from '../../knowledges/types';
405
+ import { context } from '../../../context';
406
+
407
+ export function createMyCustomProvider() {
408
+ return new DirectoryProvider({
409
+ id: 'my-custom',
410
+ name: '我的自定义内容',
411
+ description: '自定义内容描述',
412
+ weight: 50, // 设置权重
413
+ hidden: false, // 是否隐藏
414
+
415
+ async getChildren(parentId: string): Promise<IKnowledgeNode[]> {
416
+ if (parentId === 'my-custom') {
417
+ // 从 context 获取数据
418
+ const data = context.api?.custom?.getData?.();
419
+
420
+ return data.map(item => ({
421
+ id: item.id,
422
+ name: item.name,
423
+ type: KnowledgeNodeType.FILE,
424
+ extname: '.md',
425
+ metadata: { providerId: 'my-custom' }
426
+ }));
427
+ }
428
+ return [];
429
+ },
430
+
431
+ async readFile(fileId: string): Promise<string> {
432
+ // 从 context 获取文件内容
433
+ return context.api?.custom?.getContent?.(fileId) || '';
434
+ }
435
+ });
436
+ }
437
+
438
+ // workspace.ts
439
+ import { createMyCustomProvider } from './providers/my-custom-provider';
440
+
441
+ class Workspace extends KnowledgeBase {
442
+ constructor(config?: WorkspaceConfig) {
443
+ super({ name: 'Workspace', ... });
444
+
445
+ // 注册自定义 Provider
446
+ this.registerProvider(createMyCustomProvider());
447
+ }
448
+ }
449
+ ```
450
+
451
+ ### 添加新的动态文档目录
452
+
453
+ ```typescript
454
+ // 在 Workspace 构造函数中
455
+ this.registerDynamicDirectory({
456
+ id: 'custom-docs',
457
+ name: '自定义文档',
458
+ description: '自定义的动态文档',
459
+ weight: 60,
460
+ hidden: false
461
+ });
462
+
463
+ // 使用
464
+ await workspace.openDocument('doc1', {
465
+ type: '自定义',
466
+ content: '内容',
467
+ directoryId: 'custom-docs'
468
+ });
469
+ ```
470
+
471
+ ## 注意事项
472
+
473
+ 1. **从 context 获取依赖** - 确保在设计器环境中运行,context 必须已经初始化
474
+ 2. **默认自动打开** - 项目信息和聚焦信息默认自动打开,无需手动调用 openFile
475
+ 3. **动态内容获取** - 所有文件内容都是动态获取的,确保 API 能正确返回最新数据
476
+ 4. **组件依赖管理** - 打开组件文档会自动加载依赖,可能会打开多个文件
477
+ 5. **路径格式** - 路径区分大小写,使用正斜杠 `/` 分隔,如 `组件配置文档/pc.button.md`
478
+ 6. **隐藏节点** - 组件配置文档目录是隐藏的,不会在目录树中显示,但可以被打开和使用
479
+ 7. **权重排序** - Provider 按权重排序:项目信息(100) > 聚焦信息(90) > 动态文档目录(可配置) > 组件配置文档(0)
480
+ 8. **刷新机制** - 当 context.currentFocus 变化时,可以调用 `workspace.refresh()` 刷新工作空间
481
+
482
+ ## API 参考
483
+
484
+ ### Workspace 类
485
+
486
+ 继承自 `KnowledgeBase`,拥有所有知识库的方法。
487
+
488
+ #### 构造函数
489
+
490
+ ```typescript
491
+ constructor(config?: WorkspaceConfig)
492
+
493
+ interface WorkspaceConfig extends KnowledgeBaseConfig {
494
+ // 所有依赖从 context 获取,无需传入
495
+ }
496
+ ```
497
+
498
+ #### Workspace 特有方法
499
+
500
+ - `waitForReady(): Promise<void>` - 等待默认文件打开完成
501
+ - `openComponentDoc(path: string): Promise<void>` - 打开组件文档(通过路径)
502
+ - `openComponentDocByNamespace(namespace: string): Promise<void>` - 打开组件文档(通过 namespace)
503
+ - `openDocument(id: string, options: OpenDocumentOptions): Promise<void>` - 打开动态文档
504
+ - `closeDocument(id: string, directoryId?: string): void` - 关闭动态文档
505
+ - `refresh(): Promise<void>` - 刷新工作空间
506
+
507
+ #### 继承的方法(来自 KnowledgeBase)
508
+
509
+ - `registerProvider(provider: IDirectoryProvider): void` - 注册 Provider
510
+ - `registerDynamicDirectory(config: DynamicDocDirectoryConfig): void` - 注册动态文档目录
511
+ - `openFile(path: string): Promise<void>` - 打开文件
512
+ - `closeFile(path: string): void` - 关闭文件
513
+ - `isFileOpened(path: string): boolean` - 检查文件是否已打开
514
+ - `getOpenedFilePaths(): string[]` - 获取已打开的文件列表
515
+ - `clearOpenedFiles(): void` - 清空所有已打开的文件
516
+ - `getDirectoryTree(options?): Promise<string>` - 获取目录树
517
+ - `export(options?: ExportOptions): Promise<string>` - 导出为消息内容
518
+ - `findNode(path: string): Promise<KnowledgeNode | null>` - 查找节点
519
+ - `getRoot(): KnowledgeNode` - 获取根节点
520
+ - 以及所有动态文档相关方法...
521
+
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Workspace by Knowledges 主导出
3
+ *
4
+ * 基于知识库架构重构的 Workspace + Context
5
+ */
6
+
7
+ export { Workspace } from './workspace';
8
+ export * from './types';
9
+ export * from './providers';
10
+ export * from './utils';
11
+
@@ -0,0 +1,92 @@
1
+ /**
2
+ * 组件配置文档 Provider
3
+ * 提供所有组件的详细配置文档(隐藏目录)
4
+ */
5
+
6
+ import { DirectoryProvider, IKnowledgeNode, KnowledgeNodeType } from '../../knowledges/types';
7
+ import { ComponentsManager } from '../utils/components-manager';
8
+ import { context } from '../../../context';
9
+
10
+ /**
11
+ * 从 context 获取组件文档
12
+ */
13
+ function getComponentDoc(namespace: string): string {
14
+ return (context.api?.global?.api?.getComEditorPrompts ||
15
+ context.api?.uiCom?.api?.getComEditorPrompts)?.(namespace) || '';
16
+ }
17
+
18
+ /**
19
+ * 从 ComponentsManager 获取组件依赖
20
+ */
21
+ function getComponentRequires(namespace: string): string[] {
22
+ return ComponentsManager.getRequireComponents(namespace) || [];
23
+ }
24
+
25
+ /**
26
+ * 获取所有组件列表(从 ComponentsManager 获取)
27
+ */
28
+ function getAllComponents(): Array<{ namespace: string; name: string; abbreviation: string; description: string }> {
29
+ return ComponentsManager.getAllComponents();
30
+ }
31
+
32
+ /**
33
+ * 创建组件配置文档 Provider
34
+ * 提供一个隐藏的目录,包含所有组件的详细文档
35
+ */
36
+ export function createComponentDocsProvider(): DirectoryProvider {
37
+ const providerId = 'component-docs';
38
+ const providerName = '组件配置文档';
39
+
40
+ return new DirectoryProvider({
41
+ id: providerId,
42
+ name: providerName,
43
+ description: '所有组件的详细配置文档',
44
+ weight: 0, // 小权重
45
+ hidden: true, // 隐藏,不在目录树中显示
46
+
47
+ // 获取所有组件文档文件
48
+ getChildren: async (parentId: string) => {
49
+ if (parentId !== providerId) {
50
+ return [];
51
+ }
52
+
53
+ const components = getAllComponents();
54
+
55
+ return components.map(comp => {
56
+ // 使用缩写作为文件名
57
+ const fileName = comp.abbreviation || comp.namespace;
58
+
59
+ // 获取依赖组件的文件ID列表,用于关联打开
60
+ const requires = getComponentRequires(comp.namespace);
61
+ const requireFileIds = requires.map(ns => `component-doc-${ns}`);
62
+
63
+ return {
64
+ id: `component-doc-${comp.namespace}`,
65
+ name: fileName,
66
+ type: KnowledgeNodeType.FILE,
67
+ extname: '.md',
68
+ hidden: true, // 文件也是隐藏的
69
+ description: comp.description || comp.name,
70
+ metadata: {
71
+ providerId,
72
+ namespace: comp.namespace,
73
+ abbreviation: comp.abbreviation,
74
+ fullName: comp.name,
75
+ requires: requireFileIds // 添加依赖文件ID,用于关联打开
76
+ }
77
+ };
78
+ });
79
+ },
80
+
81
+ // 读取组件文档内容
82
+ readFile: async (fileId: string) => {
83
+ const match = fileId.match(/^component-doc-(.+)$/);
84
+ if (!match) {
85
+ throw new Error(`Invalid file ID: ${fileId}`);
86
+ }
87
+
88
+ const namespace = match[1];
89
+ return getComponentDoc(namespace);
90
+ }
91
+ });
92
+ }