@mybricks/plugin-ai 0.0.1

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 (37) hide show
  1. package/package.json +27 -0
  2. package/src/agents/app.ts +173 -0
  3. package/src/agents/common.ts +111 -0
  4. package/src/agents/index.ts +7 -0
  5. package/src/components/icons/index.less +8 -0
  6. package/src/components/icons/index.tsx +24 -0
  7. package/src/components/messages/index.less +806 -0
  8. package/src/components/messages/index.tsx +236 -0
  9. package/src/context/index.ts +21 -0
  10. package/src/data.ts +5 -0
  11. package/src/index.tsx +84 -0
  12. package/src/mock.ts +1267 -0
  13. package/src/startView/index.less +216 -0
  14. package/src/startView/index.tsx +229 -0
  15. package/src/tools/analyze-and-expand-prd.ts +166 -0
  16. package/src/tools/answer-user.ts +35 -0
  17. package/src/tools/focus-element.ts +47 -0
  18. package/src/tools/generate-page.ts +750 -0
  19. package/src/tools/get-component-info-by-ids.ts +166 -0
  20. package/src/tools/get-component-info.ts +53 -0
  21. package/src/tools/get-components-doc-and-prd.ts +137 -0
  22. package/src/tools/get-focus-mybricks-dsl.ts +26 -0
  23. package/src/tools/get-mybricks-dsl.ts +73 -0
  24. package/src/tools/index.ts +25 -0
  25. package/src/tools/modify-component.ts +385 -0
  26. package/src/tools/type.d.ts +12 -0
  27. package/src/tools/utils.ts +62 -0
  28. package/src/types.d.ts +65 -0
  29. package/src/view/components/header/header.less +17 -0
  30. package/src/view/components/header/header.tsx +15 -0
  31. package/src/view/components/index.ts +3 -0
  32. package/src/view/components/messages/messages.less +228 -0
  33. package/src/view/components/messages/messages.tsx +172 -0
  34. package/src/view/components/sender/sender.less +44 -0
  35. package/src/view/components/sender/sender.tsx +62 -0
  36. package/src/view/index.less +5 -0
  37. package/src/view/index.tsx +18 -0
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@mybricks/plugin-ai",
3
+ "version": "0.0.1",
4
+ "main": "src/index.tsx",
5
+ "files": [
6
+ "src",
7
+ "package.json"
8
+ ],
9
+ "author": "MyBricks Team",
10
+ "license": "",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/mybricks/plugin-ai.git"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public",
17
+ "registry": "https://registry.npmjs.org/"
18
+ },
19
+ "scripts": {},
20
+ "dependencies": {
21
+ "@ant-design/icons": "^6.1.0",
22
+ "@mybricks/rxai": "0.0.2",
23
+ "classnames": "^2.5.1",
24
+ "jsonrepair": "^3.13.1",
25
+ "marked": "^17.0.0"
26
+ }
27
+ }
@@ -0,0 +1,173 @@
1
+ import { context } from './../context';
2
+ import { MYBRICKS_TOOLS } from "./../tools"
3
+
4
+ window.plugin_ai_context = context
5
+
6
+ export const requestGeneratePageAgent = (pageId: string, pageTitle: string, params: any) => {
7
+
8
+ const prompts = context.prompts
9
+
10
+ params?.onProgress?.('start')
11
+
12
+ context.rxai.requestAI({
13
+ ...params,
14
+ message: params?.message,
15
+ key: pageId,
16
+ emits: {
17
+ write: () => { },
18
+ complete: () => {
19
+ // params?.onProgress?.("complete");
20
+ },
21
+ error: () => {
22
+ params?.onProgress?.("error");
23
+ },
24
+ cancel: () => {
25
+ // params?.onProgress?.("complete");
26
+ },
27
+ },
28
+ planList: ["generate-page-prd-and-require-component", "generate-page"],
29
+ tools: [
30
+ MYBRICKS_TOOLS.GetComponentsDocAndPrd({
31
+ allowComponents: context.api?.global?.api?.getAllComDefPrompts?.(),
32
+ examples: prompts.prdExamplesPrompts,
33
+ canvasWidth: prompts.canvasWidth,
34
+ queryComponentsDocsByNamespaces: (namespaces) => {
35
+ return namespaces.reduce((acc, cur) => {
36
+ return acc + '\n' + context.api?.uiCom?.api?.getComEditorPrompts?.(cur.namespace)
37
+ }, '')
38
+ }
39
+ }),
40
+ MYBRICKS_TOOLS.GeneratePage({
41
+ getFocusRootComponentDoc: () => context.api?.page?.api?.getPageContainerPrompts?.(pageId) as string,
42
+ getTargetId: () => pageId as string,
43
+ appendPrompt: prompts.systemAppendPrompts,
44
+ examples: prompts.generatePageActionExamplesPrompts,
45
+ onActions: (actions, status) => {
46
+ context.api?.page?.api?.updatePage?.(pageId, actions, status)
47
+
48
+ if (status === 'complete') {
49
+ params?.onProgress?.("complete");
50
+ }
51
+ },
52
+ onClearPage: () => {
53
+ context.api?.page?.api?.clearPageContent?.(pageId)
54
+ }
55
+ }),
56
+ ],
57
+ // presetMessages: [
58
+ // {
59
+ // role: 'user',
60
+ // content: `检测到聚焦位置发生变化`
61
+ // },
62
+ // {
63
+ // role: 'assistant',
64
+ // content: `当前已聚焦到${`页面(title=${pageTitle},id=${pageId})`}中,后续用户的提问,关于”这个“、“此”,甚至不提主语,都是指代此元素。
65
+ // <当前聚焦元素的内容简介>
66
+ // 内容为空
67
+ // </当前聚焦元素的内容简介>
68
+ // `
69
+ // }
70
+ // ]
71
+ });
72
+ }
73
+
74
+ async function createCanvasByAICanvas(canvasId: string, aiCanvas: any) {
75
+
76
+ function sleep(ms: number) {
77
+ return new Promise(resolve => setTimeout(resolve, ms));
78
+ }
79
+
80
+ // TOOD,之前不去掉设计器会有报错
81
+ await sleep(1000)
82
+
83
+ const pageArray = [];
84
+
85
+ for (let index = 0; index < aiCanvas.pages.length; index++) {
86
+ const page = aiCanvas.pages[index];
87
+
88
+ const pageRef = await context.api.page?.api?.createPage?.(canvasId, page.title, {
89
+ type: "normal",
90
+ title: page.title,
91
+ template: {
92
+ namespace: 'mybricks.harmony.systemPage',
93
+ deletable: false,
94
+ asRoot: true,
95
+ data: {
96
+ useTabBar: false,
97
+ },
98
+ },
99
+ inputs: [
100
+ {
101
+ id: "open",
102
+ title: "打开",
103
+ schema: {
104
+ type: "object",
105
+ },
106
+ },
107
+ ],
108
+ })
109
+ pageArray.push({ page, pageRef })
110
+ }
111
+
112
+ pageArray.forEach(async ({ page, pageRef }) => {
113
+ await requestGeneratePageAgent(pageRef.id, page.title, {
114
+ message: `标题:${page.title}
115
+ <需求>
116
+ ${page.prd}
117
+ </需求>
118
+
119
+ <样式风格>
120
+ ${aiCanvas.style}
121
+ </样式风格>
122
+ `,
123
+ onProgress: pageRef.onProgress,
124
+ id: pageRef.id,
125
+ })
126
+ })
127
+ }
128
+
129
+ export const requestGenerateCanvasAgent = (params: any) => {
130
+ return new Promise((resolve, reject) => {
131
+ params?.onProgress?.('start')
132
+
133
+ context.rxai.requestAI({
134
+ ...params,
135
+ message: params?.message,
136
+ key: params.id,
137
+ emits: {
138
+ write: () => { },
139
+ complete: () => {
140
+ params?.onProgress?.("complete");
141
+ },
142
+ error: () => {
143
+ reject('error')
144
+ params?.onProgress?.("error");
145
+ },
146
+ cancel: () => {
147
+ params?.onProgress?.("complete");
148
+ },
149
+ },
150
+ planList: ["analyze-and-expand-prd"],
151
+ tools: [
152
+ MYBRICKS_TOOLS.AnalyzeAndExpandPrd({
153
+ onProjectCreate: (projectJson) => {
154
+ if (!projectJson || !projectJson.title) {
155
+ return reject('不合法的项目文件')
156
+ }
157
+ let canvasId
158
+ try {
159
+ const { id, title } = context.api.page?.api?.createCanvas?.()
160
+ canvasId = id
161
+ } catch (error) {
162
+ reject(error)
163
+ }
164
+ resolve('complete')
165
+ if (canvasId) {
166
+ createCanvasByAICanvas(canvasId, projectJson);
167
+ }
168
+ }
169
+ })
170
+ ],
171
+ });
172
+ })
173
+ }
@@ -0,0 +1,111 @@
1
+ import { context } from './../context';
2
+ import { MYBRICKS_TOOLS, MyBricksHelper } from "./../tools"
3
+
4
+
5
+ export const requestCommonAgent = (params: any) => {
6
+
7
+ return new Promise((resolve, reject) => {
8
+ const prompts = context.prompts;
9
+
10
+ const targetId = context.currentFocus?.type === 'uiCom' ? context.currentFocus?.comId : context.currentFocus?.pageId
11
+
12
+ params?.onProgress?.('start')
13
+
14
+ context.rxai.requestAI({
15
+ ...params,
16
+ message: params?.message,
17
+ key: targetId,
18
+ emits: {
19
+ write: () => { },
20
+ complete: () => {
21
+ resolve('complete')
22
+ params?.onProgress?.("complete");
23
+ },
24
+ error: () => {
25
+ reject('error')
26
+ params?.onProgress?.("error");
27
+ },
28
+ cancel: () => {},
29
+ },
30
+ tools: [
31
+ MYBRICKS_TOOLS.GetComponentsDocAndPrd({
32
+ allowComponents: context.api?.global?.api?.getAllComDefPrompts?.(),
33
+ examples: prompts.prdExamplesPrompts,
34
+ canvasWidth: prompts.canvasWidth,
35
+ queryComponentsDocsByNamespaces: (namespaces) => {
36
+ return namespaces.reduce((acc, cur) => {
37
+ return acc + '\n' + context.api?.uiCom?.api?.getComEditorPrompts?.(cur.namespace)
38
+ }, '')
39
+ }
40
+ }),
41
+ MYBRICKS_TOOLS.GeneratePage({
42
+ getFocusRootComponentDoc: () => context.api?.page?.api?.getPageContainerPrompts?.(targetId) as string,
43
+ getTargetId: () => targetId as string,
44
+ appendPrompt: prompts.systemAppendPrompts,
45
+ examples: prompts.generatePageActionExamplesPrompts,
46
+ onActions: (actions, status) => {
47
+ context.api?.page?.api?.updatePage?.(targetId, actions, status)
48
+ },
49
+ onClearPage: () => {
50
+ context.api?.page?.api?.clearPageContent?.(targetId)
51
+ }
52
+ }),
53
+ // MYBRICKS_TOOLS.GetMybricksDSL({
54
+ // getContext: (id, type) => {
55
+ // if (type === 'page') {
56
+ // return api?.page?.api?.getPageDSLPrompts?.(id)?.toDSL?.()?.replaceAll(`slots.${id}`, 'canvas') as string
57
+ // }
58
+ // return api?.uiCom?.api?.getComDSLPrompts?.(id) as string
59
+ // },
60
+ // }),
61
+ MYBRICKS_TOOLS.GetFocusMybricksDSL({
62
+ id: targetId as string,
63
+ getFocusContext() {
64
+ if (context.currentFocus?.type === 'page') {
65
+ return context.api?.page?.api?.getPageDSLPrompts?.(targetId)?.toDSL?.()?.replaceAll(`slots.${targetId}`, 'canvas') as string
66
+ }
67
+ return context.api?.uiCom?.api?.getComDSLPrompts?.(targetId) as string
68
+ },
69
+ }),
70
+ MYBRICKS_TOOLS.GetComponentsInfoByIds({
71
+ getPageJson(id) {
72
+ return context.api?.page?.api?.getOutlineInfo(id)
73
+ },
74
+ getComInfo(namespace) {
75
+ return context.api?.uiCom?.api?.getComEditorPrompts?.(namespace)
76
+ },
77
+ getComJson(id) {
78
+ return context.api?.uiCom?.api?.getOutlineInfo(id)
79
+ },
80
+ }),
81
+ // MYBRICKS_TOOLS.GetComponentInfo({
82
+ // getComInfo(id) {
83
+ // return context.api?.uiCom?.api?.getComPrompts?.(id)?.replace(/当前组件的情况/g, `组件${id}的信息`) as string
84
+ // },
85
+ // }),
86
+ MYBRICKS_TOOLS.ModifyComponent({
87
+ onActions: (id, actions) => {
88
+ context.api?.uiCom?.api?.updateCom?.(id, actions)
89
+ }
90
+ }),
91
+ ],
92
+ presetMessages: [
93
+ {
94
+ role: 'user',
95
+ content: `检测到聚焦位置发生变化`
96
+ },
97
+ {
98
+ role: 'assistant',
99
+ content: `当前已聚焦到${context.currentFocus?.type === 'uiCom' ? `组件(id=${context.currentFocus?.comId})` : `页面(title=${context.currentFocus?.title},id=${context.currentFocus?.pageId})`}中,后续用户的提问,关于”这个“、“此”,甚至不提主语,都是指代此元素。
100
+ <当前聚焦元素的内容简介>
101
+ ${MyBricksHelper.getTreeDescriptionByJson(context.currentFocus?.type === 'uiCom' ? context.api?.uiCom?.api?.getOutlineInfo(context.currentFocus?.comId) : context.api?.page?.api?.getOutlineInfo(context.currentFocus?.pageId))}
102
+
103
+ > 如果内容不为空,代表组件通过插槽放置有子组件,如果内容为空,则代表此组件没有任何子组件。
104
+ </当前聚焦元素的内容简介>
105
+ `
106
+ }
107
+ ]
108
+ });
109
+ })
110
+
111
+ }
@@ -0,0 +1,7 @@
1
+ import { requestGenerateCanvasAgent } from './app'
2
+ import { requestCommonAgent } from './common'
3
+
4
+ export const Agents = {
5
+ requestGenerateCanvasAgent,
6
+ requestCommonAgent
7
+ }
@@ -0,0 +1,8 @@
1
+ .spin {
2
+ animation: spin 1.2s linear infinite;
3
+ }
4
+
5
+ @keyframes spin{
6
+ from { transform: rotate(0deg); }
7
+ to { transform: rotate(360deg); }
8
+ }
@@ -0,0 +1,24 @@
1
+ import React from "react";
2
+ import css from "./index.less";
3
+
4
+ const ArrowUp = () => {
5
+ return <svg data-v-dbdac3f2="" xmlns="http://www.w3.org/2000/svg" role="img" viewBox="0 0 1024 1024" name="Send"><path d="M705.536 433.664a38.4 38.4 0 1 1-54.272 54.272L550.4 387.114667V729.6a38.4 38.4 0 0 1-76.8 0V387.114667l-100.864 100.821333a38.4 38.4 0 1 1-54.272-54.272l166.4-166.4a38.4 38.4 0 0 1 54.272 0l166.4 166.4z" fill="currentColor"></path></svg>
6
+ }
7
+
8
+ const Attachment = () => {
9
+ return <svg data-v-5d21dc8d="" xmlns="http://www.w3.org/2000/svg" role="img" viewBox="0 0 1024 1024" name="Clip"><path d="M239.08352 319.0784a188.17024 188.17024 0 0 1 376.29952 0v0.16384l4.62848 347.62752v0.08192a112.64 112.64 0 0 1-156.0576 105.63584 112.55808 112.55808 0 0 1-68.97664-105.39008V315.14624a36.864 36.864 0 1 1 73.728 0v352.99328a38.83008 38.83008 0 1 0 77.57824 0v-0.16384l-4.58752-347.58656V320.3072a114.4832 114.4832 0 0 0-228.88448-0.4096l4.5056 347.58656a190.13632 190.13632 0 1 0 380.3136 0l0.4096-334.39744a36.864 36.864 0 1 1 73.728 0.08192l-0.4096 334.31552a263.90528 263.90528 0 0 1-450.43712 186.61376 263.86432 263.86432 0 0 1-77.29152-186.368l-4.54656-347.50464v-1.10592z" fill="currentColor"></path></svg>
10
+ }
11
+
12
+ const Loading = () => {
13
+ return <svg className={css.spin} viewBox="0 0 1024 1024" focusable="false" data-icon="loading-3-quarters" fill="currentColor" aria-hidden="true"><path d="M512 1024c-69.1 0-136.2-13.5-199.3-40.2C251.7 958 197 921 150 874c-47-47-84-101.7-109.8-162.7C13.5 648.2 0 581.1 0 512c0-19.9 16.1-36 36-36s36 16.1 36 36c0 59.4 11.6 117 34.6 171.3 22.2 52.4 53.9 99.5 94.3 139.9 40.4 40.4 87.5 72.2 139.9 94.3C395 940.4 452.6 952 512 952c59.4 0 117-11.6 171.3-34.6 52.4-22.2 99.5-53.9 139.9-94.3 40.4-40.4 72.2-87.5 94.3-139.9C940.4 629 952 571.4 952 512c0-59.4-11.6-117-34.6-171.3a440.45 440.45 0 00-94.3-139.9 437.71 437.71 0 00-139.9-94.3C629 83.6 571.4 72 512 72c-19.9 0-36-16.1-36-36s16.1-36 36-36c69.1 0 136.2 13.5 199.3 40.2C772.3 66 827 103 874 150c47 47 83.9 101.8 109.7 162.7 26.7 63.1 40.2 130.2 40.2 199.3s-13.5 136.2-40.2 199.3C958 772.3 921 827 874 874c-47 47-101.8 83.9-162.7 109.7-63.1 26.8-130.2 40.3-199.3 40.3z"></path></svg>
14
+ }
15
+
16
+ const Close = () => {
17
+ return <svg fill-rule="evenodd" viewBox="64 64 896 896" focusable="false" data-icon="close-circle" fill="currentColor" aria-hidden="true"><path d="M512 64c247.4 0 448 200.6 448 448S759.4 960 512 960 64 759.4 64 512 264.6 64 512 64zm0 76c-205.4 0-372 166.6-372 372s166.6 372 372 372 372-166.6 372-372-166.6-372-372-372zm128.01 198.83c.03 0 .05.01.09.06l45.02 45.01a.2.2 0 01.05.09.12.12 0 010 .07c0 .02-.01.04-.05.08L557.25 512l127.87 127.86a.27.27 0 01.05.06v.02a.12.12 0 010 .07c0 .03-.01.05-.05.09l-45.02 45.02a.2.2 0 01-.09.05.12.12 0 01-.07 0c-.02 0-.04-.01-.08-.05L512 557.25 384.14 685.12c-.04.04-.06.05-.08.05a.12.12 0 01-.07 0c-.03 0-.05-.01-.09-.05l-45.02-45.02a.2.2 0 01-.05-.09.12.12 0 010-.07c0-.02.01-.04.06-.08L466.75 512 338.88 384.14a.27.27 0 01-.05-.06l-.01-.02a.12.12 0 010-.07c0-.03.01-.05.05-.09l45.02-45.02a.2.2 0 01.09-.05.12.12 0 01.07 0c.02 0 .04.01.08.06L512 466.75l127.86-127.86c.04-.05.06-.06.08-.06a.12.12 0 01.07 0z"></path></svg>
18
+ }
19
+
20
+ const Success = () => {
21
+ return <svg role="presentation" xmlns="http://www.w3.org/2000/svg" fill="none" version="1.1" viewBox="0 0 14 14"><path d="M12.25,7.000000238418579C12.25,9.899500238418579,9.8995,12.250000238418579,7,12.250000238418579C4.1005,12.250000238418579,1.75,9.899500238418579,1.75,7.000000238418579C1.75,4.100510238418579,4.1005,1.750000238418579,7,1.750000238418579C9.8995,1.750000238418579,12.25,4.100510238418579,12.25,7.000000238418579ZM11.08333,7.000000238418579C11.08333,4.744840238418579,9.25516,2.916670238418579,7,2.916670238418579C4.74484,2.916670238418579,2.91667,4.744840238418579,2.91667,7.000000238418579C2.91667,9.25516023841858,4.74484,11.08333023841858,7,11.08333023841858C9.25516,11.08333023841858,11.08333,9.25516023841858,11.08333,7.000000238418579ZM9.20146,6.202720238418579C9.28673,6.098500238418579,9.33333,5.9679902384185795,9.33333,5.833330238418579C9.33333,5.511170238418579,9.07217,5.250000238418579,8.75,5.250000238418579C8.575040000000001,5.250000238418579,8.4093,5.32853023841858,8.2984,5.464070238418579L6.37337,7.298400238418579L5.6624300000000005,6.587530238418579C5.55304,6.478130238418579,5.40471,6.416670238418579,5.25,6.416670238418579C4.92783,6.416670238418579,4.66667,6.677830238418579,4.66667,7.000000238418579C4.66667,7.154710238418579,4.72809,7.303090238418579,4.8377099999999995,7.412700238418579L6.00423,8.579140238418578C6.01794,8.59285023841858,6.03224,8.605870238418579,6.04724,8.61814023841858C6.29658,8.82215023841858,6.66412,8.785400238418578,6.86812,8.536060238418578L9.20146,6.202720238418579Z" fill="currentColor"></path></svg>
22
+ }
23
+
24
+ export { ArrowUp, Attachment, Loading, Close, Success }