@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
@@ -2,7 +2,9 @@ interface AiServiceAPI {
2
2
  global: {
3
3
  title: string;
4
4
  api: {
5
+ getAllPageInfo: () => {pageAry: any[]}[];
5
6
  getAllComDefPrompts: () => string;
7
+ getComEditorPrompts: (...params: any) => string;
6
8
  }
7
9
  };
8
10
  page: {
@@ -16,6 +18,7 @@ interface AiServiceAPI {
16
18
  getOutlineInfo: (...params: any) => any
17
19
  createCanvas: () => { id: string; title: string; }
18
20
  createPage: (id: string, title: string, config?: any) => { id: string; onProgress: Function; }
21
+ getPageOnProcess: (...params: any) => any;
19
22
  }
20
23
  };
21
24
  uiCom: {
@@ -24,20 +27,44 @@ interface AiServiceAPI {
24
27
  updateCom: (...params: any) => void;
25
28
  getComPrompts: (...params: any) => string;
26
29
  getComDSLPrompts: (...params: any) => string;
30
+ /** @deprecated 废弃 */
27
31
  getComEditorPrompts: (...params: any) => string;
28
32
  getOutlineInfo: (...params: any) => any
33
+ getComOnProcess: (...params: any) => any;
34
+ }
35
+ }
36
+ diagram: {
37
+ title: string;
38
+ api: {
39
+ createDiagram: (...args: any) => { id: string; title: string }
40
+ updateDiagram: (...args: any) => void;
41
+ getDiagramInfo: (...args: any) => any;
42
+ getDiagramInfoByVarId: (...args: any) => any;
43
+ getDiagramInfoByListenerInfo: (...args: any) => any;
44
+ }
45
+ },
46
+ logicCom: {
47
+ title: string;
48
+ api: {
49
+ getOutlineInfo: (...params: any) => any;
50
+ updateCom: (...params: any) => any;
29
51
  }
30
52
  }
31
53
  }
32
54
 
33
55
  type AiServiceFocusParams = {
34
- type: "uiCom";
56
+ onProgress: (status: "start" | "ing" | "complete") => void;
57
+ /** 区域才会有 */
58
+ focusArea?: {
59
+ selector: string;
60
+ title: string;
61
+ }
35
62
  comId: string;
36
- title: string;
37
- } | {
38
- type: "page";
39
63
  pageId: string;
40
64
  title: string;
65
+ /** 类型,组件、页面,或自定义类型 */
66
+ type: "page" | "uiCom" | "section" | "logicCom" | string;
67
+ vibeCoding?: boolean;
41
68
  }
42
69
 
43
70
  type AiServiceRequestParams = {
@@ -61,5 +88,13 @@ type AiServiceRequestParams = {
61
88
  title?: string;
62
89
  size?: number;
63
90
  }[];
64
- onProgress: (status: string) => void;
91
+ onProgress: (status: "start" | "ing" | "complete") => void;
65
92
  }
93
+ interface AiViewApi {
94
+ focusPage: (pageId: string) => void;
95
+ focusCom: (comId: string) => void;
96
+ }
97
+
98
+ interface AiStartViewApi {
99
+ onProgress: (state: "start" | "ing") => void;
100
+ }
package/src/index.tsx CHANGED
@@ -1,7 +1,9 @@
1
1
  import React from 'react';
2
- import { Rxai } from "@mybricks/rxai";
3
2
  import data from './data';
4
3
 
4
+ import './agents/workspace-by-knowledges/test';
5
+ import './../test'
6
+
5
7
  import pkg from '../package.json';
6
8
 
7
9
  console.log(`%c ${pkg.name} %c@${pkg.version}`, `color:#FFF;background:#fa6400`, ``, ``);
@@ -10,30 +12,84 @@ import { Agents } from './agents'
10
12
  import { View } from "./view";
11
13
  import { context } from './context';
12
14
  import { StartView } from "./startView";
15
+ import { DeviceType } from './types';
16
+ import { createGetAllComDefPrompts } from "./api/cloud-components";
17
+ import { AgentConfigParams, getAgentConfigs, backStoryPrompts, transformLegacyPromptsToAgents, AbstractAgent, CustomAgent } from './agents/utils/config';
18
+
19
+ import preset from "./preset"
20
+ import { apiRecorder } from './api-record-replay';
21
+ import { replay, replayFromJSON, ReplayAPI, ReplayOptions } from './api-record-replay';
22
+ import { RecordedAction } from './api-record-replay';
23
+ import { fileFormat } from '@mybricks/rxai';
24
+
25
+ // 导出收集和回放相关的接口
26
+ export { apiRecorder, replay, replayFromJSON, fileFormat };
27
+ export type { RecordedAction, ReplayAPI, ReplayOptions };
28
+
29
+ const transformParams = (params: any = {}) => {
30
+ const result: any = {...preset};
31
+ Object.entries(params).forEach(([key, value]: any) => {
32
+ if (key === "agents") {
33
+ result[key] = preset.agents.concat(value);
34
+ } else {
35
+ result[key] = value;
36
+ }
37
+ })
38
+ return Object.assign({...preset}, params);
39
+ }
13
40
 
14
- export { fileFormat } from '@mybricks/rxai'
41
+ export default function pluginAI(params?: any): any {
42
+ const {
43
+ name = '智能助手',
44
+ user,
45
+ prompts,
46
+ mock,
47
+ key,
48
+ agents: rawAgents,
49
+ guidePrompt,
50
+ createTemplates,
51
+ isMutiCanvas,
52
+ deviceType,
53
+ config,
54
+ mode = 'development',
55
+ } = transformParams(params);
15
56
 
16
- export default function pluginAI({
17
- user = {
18
- name: "user",
19
- avatar: "/default_avatar.png"
20
- },
21
- requestAsStream,
22
- prompts
23
- }: any): any {
57
+ const requestAsStream = preset.createRequestAsStream(mode);
24
58
 
25
59
  const copilot = {
26
- name: "MyBricks.ai",
60
+ // name: "MyBricks.ai",
61
+ name: "智能助手",
27
62
  avatar: "https://my.mybricks.world/image/icon.png"
28
63
  }
29
64
 
30
- context.prompts = prompts
65
+ // 兼容历史配置:如果没有 agents 但有 prompts,则将 prompts 转换为 agents
66
+ const agents = rawAgents || (prompts ? transformLegacyPromptsToAgents(prompts) : undefined);
31
67
 
32
- // window.requestGenerateCanvasAgent = requestGenerateCanvasAgent
68
+ context.name = name;
69
+ context.agents = agents;
70
+ context.createTemplates = createTemplates ?? {};
71
+ context.isMutiCanvas = isMutiCanvas ?? true;
72
+ context.deviceType = deviceType ?? DeviceType.Mobile;
73
+ context.userConfig = config ?? {}
74
+ context.pluginParams = {
75
+ name,
76
+ user,
77
+ prompts,
78
+ requestAsStream,
79
+ mock,
80
+ key,
81
+ agents: rawAgents,
82
+ guidePrompt,
83
+ createTemplates,
84
+ isMutiCanvas,
85
+ deviceType,
86
+ config,
87
+ mode,
88
+ }
33
89
 
34
90
  return {
35
91
  name: '@mybricks/plugins/ai',
36
- title: 'MyBricksAI助手',
92
+ title: name,
37
93
  author: 'MyBricks',
38
94
  ['author.zh']: 'MyBricks',
39
95
  version: '1.0.0',
@@ -43,42 +99,166 @@ export default function pluginAI({
43
99
  aiService: {
44
100
  init(api: AiServiceAPI) {
45
101
  context.api = api;
102
+
103
+ // 在 context.designer 上挂载新的 API(带记录功能)
104
+ context.designer = {
105
+ createPage: async (id: string, title: string, config?: any) => {
106
+ const params = [id, title, config];
107
+ apiRecorder.record('createPage', params);
108
+ return api.page.api.createPage(id, title, config);
109
+ },
110
+ createCanvas: async () => {
111
+ const params: any[] = [];
112
+ apiRecorder.record('createCanvas', params);
113
+ return api.page.api.createCanvas();
114
+ },
115
+ updatePage: async (...params: any[]) => {
116
+ apiRecorder.record('updatePage', params);
117
+ return api.page.api.updatePage(...params);
118
+ },
119
+ updateUiCom: async (...params: any[]) => {
120
+ apiRecorder.record('updateUiCom', params);
121
+ return api.uiCom.api.updateCom(...params);
122
+ },
123
+ updateLogicCom: async (...params: any[]) => {
124
+ apiRecorder.record('updateLogicCom', params);
125
+ return api.logicCom.api.updateCom(...params);
126
+ },
127
+ createDiagram: async (...params: any[]) => {
128
+ apiRecorder.record('createDiagram', params);
129
+ return api.diagram.api.createDiagram(...params);
130
+ },
131
+ updateDiagram: async (...params: any[]) => {
132
+ apiRecorder.record('updateDiagram', params);
133
+ return api.diagram.api.updateDiagram(...params);
134
+ },
135
+ getDiagramInfo: (...params: any[]) => {
136
+ return api.diagram.api.getDiagramInfo(...params);
137
+ },
138
+ getDiagramInfoByVarId: (...params: any[]) => {
139
+ return api.diagram.api.getDiagramInfoByVarId(...params);
140
+ },
141
+ getDiagramInfoByListenerInfo: (...params: any[]) => {
142
+ return api.diagram.api.getDiagramInfoByListenerInfo(...params);
143
+ },
144
+ getAllComDefPrompts: createGetAllComDefPrompts(api?.global?.api?.getAllComDefPrompts)
145
+ };
146
+
147
+ const useMock = !!mock?.length;
148
+
149
+ const mockRequestAsStream = () => {
150
+ let count = 0;
151
+ let length = mock.length;
152
+ return (params: {
153
+ messages: any;
154
+ emits: any;
155
+ aiRole?: any;
156
+ }) => {
157
+ params.emits.write("");
158
+ params.emits.write(mock[count++]);
159
+ params.emits.complete("");
160
+ if (count === length) {
161
+ count = 0;
162
+ }
163
+ }
164
+ }
165
+
46
166
  context.createRxai({
167
+ system: {
168
+ // title: 'MyBricks.ai',
169
+ title: '智能助手',
170
+ prompt: getAgentConfigs(agents, 'page')?.system ?? backStoryPrompts()
171
+ },
47
172
  request: {
48
- requestAsStream
49
- }
173
+ maxRetries: 3,
174
+ requestAsStream: useMock ? mockRequestAsStream() : requestAsStream
175
+ },
176
+ key
50
177
  })
51
178
 
52
179
  console.log("[init - API]", api)
53
180
 
181
+ window._registerAgent_ = (agentConfig: any) => {
182
+ console.log('registerAgent', agentConfig)
183
+ context.agents.push(new CustomAgent(agentConfig));
184
+ }
185
+
54
186
  return {
55
187
  focus(params: AiServiceFocusParams) {
56
- if (!params) {
57
- context.currentFocus = undefined;
58
- return;
59
- }
60
- context.currentFocus = params;
188
+ const currentFocus = !params ? undefined : params;
189
+ context.currentFocus = currentFocus;
190
+ context.events.emit("focus", currentFocus);
191
+ console.log('focus', currentFocus)
61
192
  },
62
- request(params: AiServiceRequestParams) {
63
- if (params.attachments?.length) {
64
- params.message = "参考附件图片完成页面搭建\n" + params.message
193
+ request(requestParams: AiServiceRequestParams) {
194
+ if (requestParams.attachments?.length) {
195
+ // TODO: attachments是Proxy代理,引擎不应该抛出此类代理
196
+ requestParams.attachments = requestParams.attachments.map((attachment) => {
197
+ return {
198
+ ...attachment
199
+ }
200
+ })
201
+ } else {
202
+ requestParams.attachments = [];
65
203
  }
66
204
 
67
- Agents.requestCommonAgent(params)
68
- }
205
+ const focus = context.currentFocus;
206
+ const extension: any = {};
207
+
208
+ if (focus) {
209
+ const type = focus.type;
210
+ const id = type === "page" ? focus.pageId : focus.comId;
211
+ extension.mentions = [
212
+ {
213
+ id,
214
+ type,
215
+ name: focus.title,
216
+ }
217
+ ]
218
+ // TODO: 兼容引擎的onProgress问题
219
+ if (focus.onProgress) {
220
+ requestParams.onProgress = focus.onProgress;
221
+ } else if (requestParams.onProgress) {
222
+ focus.onProgress = requestParams.onProgress;
223
+ }
224
+ }
225
+
226
+ // 使用统一的 requestAgent 方法,自动处理自定义 agent 和默认 agent
227
+ const focusId = focus ? (focus.type === "page" ? focus.pageId : focus.comId) : "";
228
+ context.requestStatusTracker.track(focusId, Agents.requestAgent({ ...requestParams, extension, agents, guidePrompt }))
229
+ },
230
+ registerAgent: window._registerAgent_,
231
+ fileFormat
69
232
  }
70
233
  }
71
234
  },
72
235
  aiView: {
73
- render() {
74
- return <View user={user} copilot={copilot}/>
75
- }
236
+ render(api: AiViewApi) {
237
+ context.aiViewAPI = api;
238
+ return <View user={user} copilot={copilot} api={api}/>
239
+ },
240
+ display() {
241
+ context.events.emit("aiViewDisplay", true);
242
+ },
243
+ hide() {}
76
244
  },
77
245
  aiStartView: {
78
- render() {
79
- return <StartView user={user} copilot={copilot}/>
246
+ render(api: AiStartViewApi) {
247
+ return <StartView user={user} copilot={copilot} api={api}/>
80
248
  }
81
249
  }
82
250
  }
83
251
  }
84
- }
252
+ }
253
+
254
+ export { MyBricksParamsTools as MyBricksTools, Agent } from './agents/utils/config';
255
+ interface AgentPluginProps {
256
+ agents: AgentConfigParams[];
257
+ [key: string]: any;
258
+ }
259
+
260
+ export function agentPlugin(props: AgentPluginProps) {
261
+ return pluginAI(props)
262
+ }
263
+
264
+ export { AbstractAgent }