@mybricks/plugin-ai 0.0.1 → 0.0.3

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 (203) hide show
  1. package/dist/_virtual/_rollupPluginBabelHelpers.js +164 -0
  2. package/dist/_virtual/_rollupPluginBabelHelpers.js.map +1 -0
  3. package/dist/agents/app.js +335 -0
  4. package/dist/agents/app.js.map +1 -0
  5. package/dist/agents/common.js +483 -0
  6. package/dist/agents/common.js.map +1 -0
  7. package/dist/agents/custom.js +16 -0
  8. package/dist/agents/custom.js.map +1 -0
  9. package/dist/agents/index.js +38 -0
  10. package/dist/agents/index.js.map +1 -0
  11. package/dist/agents/knowledges/knowledge-base.js +579 -0
  12. package/dist/agents/knowledges/knowledge-base.js.map +1 -0
  13. package/dist/agents/knowledges/knowledge-node.js +228 -0
  14. package/dist/agents/knowledges/knowledge-node.js.map +1 -0
  15. package/dist/agents/knowledges/types.js +105 -0
  16. package/dist/agents/knowledges/types.js.map +1 -0
  17. package/dist/agents/utils/config.js +229 -0
  18. package/dist/agents/utils/config.js.map +1 -0
  19. package/dist/agents/workspace/coding-manager.js +20 -0
  20. package/dist/agents/workspace/coding-manager.js.map +1 -0
  21. package/dist/agents/workspace/components-manager.js +113 -0
  22. package/dist/agents/workspace/components-manager.js.map +1 -0
  23. package/dist/agents/workspace/outline-focus.js +167 -0
  24. package/dist/agents/workspace/outline-focus.js.map +1 -0
  25. package/dist/agents/workspace/outline-info.js +430 -0
  26. package/dist/agents/workspace/outline-info.js.map +1 -0
  27. package/dist/agents/workspace/page-tree-generator.js +70 -0
  28. package/dist/agents/workspace/page-tree-generator.js.map +1 -0
  29. package/dist/agents/workspace/workspace.js +251 -0
  30. package/dist/agents/workspace/workspace.js.map +1 -0
  31. package/dist/agents/workspace-by-knowledges/providers/component-docs-provider.js +100 -0
  32. package/dist/agents/workspace-by-knowledges/providers/component-docs-provider.js.map +1 -0
  33. package/dist/agents/workspace-by-knowledges/providers/focus-info-provider.js +120 -0
  34. package/dist/agents/workspace-by-knowledges/providers/focus-info-provider.js.map +1 -0
  35. package/dist/agents/workspace-by-knowledges/providers/project-info-provider.js +146 -0
  36. package/dist/agents/workspace-by-knowledges/providers/project-info-provider.js.map +1 -0
  37. package/dist/agents/workspace-by-knowledges/test.js +242 -0
  38. package/dist/agents/workspace-by-knowledges/test.js.map +1 -0
  39. package/dist/agents/workspace-by-knowledges/utils/components-manager.js +132 -0
  40. package/dist/agents/workspace-by-knowledges/utils/components-manager.js.map +1 -0
  41. package/dist/agents/workspace-by-knowledges/utils/outline-focus.js +167 -0
  42. package/dist/agents/workspace-by-knowledges/utils/outline-focus.js.map +1 -0
  43. package/dist/agents/workspace-by-knowledges/utils/outline-info.js +430 -0
  44. package/dist/agents/workspace-by-knowledges/utils/outline-info.js.map +1 -0
  45. package/dist/agents/workspace-by-knowledges/workspace.js +169 -0
  46. package/dist/agents/workspace-by-knowledges/workspace.js.map +1 -0
  47. package/dist/api/cloud-components.js +85 -0
  48. package/dist/api/cloud-components.js.map +1 -0
  49. package/dist/api-record-replay/manager.js +168 -0
  50. package/dist/api-record-replay/manager.js.map +1 -0
  51. package/dist/api-record-replay/recorder.js +107 -0
  52. package/dist/api-record-replay/recorder.js.map +1 -0
  53. package/dist/api-record-replay/replayer.js +136 -0
  54. package/dist/api-record-replay/replayer.js.map +1 -0
  55. package/dist/components/attachments/index.js +124 -0
  56. package/dist/components/attachments/index.js.map +1 -0
  57. package/dist/components/attachments/index.less.js +8 -0
  58. package/dist/components/attachments/index.less.js.map +1 -0
  59. package/dist/components/icons/index.js +112 -0
  60. package/dist/components/icons/index.js.map +1 -0
  61. package/dist/components/icons/index.less.js +8 -0
  62. package/dist/components/icons/index.less.js.map +1 -0
  63. package/dist/components/mention/index.js +19 -0
  64. package/dist/components/mention/index.js.map +1 -0
  65. package/dist/components/mention/index.less.js +8 -0
  66. package/dist/components/mention/index.less.js.map +1 -0
  67. package/dist/components/messages/index.js +429 -0
  68. package/dist/components/messages/index.js.map +1 -0
  69. package/dist/components/messages/index.less.js +8 -0
  70. package/dist/components/messages/index.less.js.map +1 -0
  71. package/dist/components/sender/index.js +244 -0
  72. package/dist/components/sender/index.js.map +1 -0
  73. package/dist/components/sender/index.less.js +8 -0
  74. package/dist/components/sender/index.less.js.map +1 -0
  75. package/dist/context/RequestStatusTracker.js +44 -0
  76. package/dist/context/RequestStatusTracker.js.map +1 -0
  77. package/dist/context/index.js +45 -0
  78. package/dist/context/index.js.map +1 -0
  79. package/dist/data.js +4 -0
  80. package/dist/data.js.map +1 -0
  81. package/dist/index.js +306 -0
  82. package/dist/index.js.map +1 -0
  83. package/dist/plugin-ai/node_modules/style-inject/dist/style-inject.es.js +27 -0
  84. package/dist/plugin-ai/node_modules/style-inject/dist/style-inject.es.js.map +1 -0
  85. package/dist/plugin-ai/package.json.js +8 -0
  86. package/dist/plugin-ai/package.json.js.map +1 -0
  87. package/dist/plugin-ai/test/index.js +206 -0
  88. package/dist/plugin-ai/test/index.js.map +1 -0
  89. package/dist/preset/createTemplates.js +29 -0
  90. package/dist/preset/createTemplates.js.map +1 -0
  91. package/dist/preset/index.js +13 -0
  92. package/dist/preset/index.js.map +1 -0
  93. package/dist/preset/prompts.js +34 -0
  94. package/dist/preset/prompts.js.map +1 -0
  95. package/dist/preset/user.js +7 -0
  96. package/dist/preset/user.js.map +1 -0
  97. package/dist/requestAsStream.js +269 -0
  98. package/dist/requestAsStream.js.map +1 -0
  99. package/dist/rxai/node_modules/idb/build/index.js +277 -0
  100. package/dist/rxai/node_modules/idb/build/index.js.map +1 -0
  101. package/dist/rxai/src/agent/base.js +16 -0
  102. package/dist/rxai/src/agent/base.js.map +1 -0
  103. package/dist/rxai/src/agent/planning.js +1076 -0
  104. package/dist/rxai/src/agent/planning.js.map +1 -0
  105. package/dist/rxai/src/agent/rxai.js +254 -0
  106. package/dist/rxai/src/agent/rxai.js.map +1 -0
  107. package/dist/rxai/src/error/base.js +39 -0
  108. package/dist/rxai/src/error/base.js.map +1 -0
  109. package/dist/rxai/src/prompt/base.js +11 -0
  110. package/dist/rxai/src/prompt/base.js.map +1 -0
  111. package/dist/rxai/src/prompt/planning.js +23 -0
  112. package/dist/rxai/src/prompt/planning.js.map +1 -0
  113. package/dist/rxai/src/prompt/tool.js +14 -0
  114. package/dist/rxai/src/prompt/tool.js.map +1 -0
  115. package/dist/rxai/src/request/request.js +123 -0
  116. package/dist/rxai/src/request/request.js.map +1 -0
  117. package/dist/rxai/src/tool/getHistoryRecords.js +65 -0
  118. package/dist/rxai/src/tool/getHistoryRecords.js.map +1 -0
  119. package/dist/rxai/src/tool/util.js +424 -0
  120. package/dist/rxai/src/tool/util.js.map +1 -0
  121. package/dist/rxai/src/utils/events.js +43 -0
  122. package/dist/rxai/src/utils/events.js.map +1 -0
  123. package/dist/rxai/src/utils/idb.js +240 -0
  124. package/dist/rxai/src/utils/idb.js.map +1 -0
  125. package/dist/rxai/src/utils/retry.js +32 -0
  126. package/dist/rxai/src/utils/retry.js.map +1 -0
  127. package/dist/rxai/src/utils/uuid.js +7 -0
  128. package/dist/rxai/src/utils/uuid.js.map +1 -0
  129. package/dist/startView/components/header/header.js +12 -0
  130. package/dist/startView/components/header/header.js.map +1 -0
  131. package/dist/startView/components/header/header.less.js +8 -0
  132. package/dist/startView/components/header/header.less.js.map +1 -0
  133. package/dist/startView/index.js +62 -0
  134. package/dist/startView/index.js.map +1 -0
  135. package/dist/startView/index.less.js +8 -0
  136. package/dist/startView/index.less.js.map +1 -0
  137. package/dist/tools/analyze-and-expand-prd.js +116 -0
  138. package/dist/tools/analyze-and-expand-prd.js.map +1 -0
  139. package/dist/tools/analyze-requirement-and-components.js +208 -0
  140. package/dist/tools/analyze-requirement-and-components.js.map +1 -0
  141. package/dist/tools/answer.js +33 -0
  142. package/dist/tools/answer.js.map +1 -0
  143. package/dist/tools/build-process.js +550 -0
  144. package/dist/tools/build-process.js.map +1 -0
  145. package/dist/tools/coding-subagent-as-tool.js +101 -0
  146. package/dist/tools/coding-subagent-as-tool.js.map +1 -0
  147. package/dist/tools/generate-ui-content.js +528 -0
  148. package/dist/tools/generate-ui-content.js.map +1 -0
  149. package/dist/tools/index.js +31 -0
  150. package/dist/tools/index.js.map +1 -0
  151. package/dist/tools/open-dsl.js +49 -0
  152. package/dist/tools/open-dsl.js.map +1 -0
  153. package/dist/tools/refactor-ui-content.js +292 -0
  154. package/dist/tools/refactor-ui-content.js.map +1 -0
  155. package/dist/tools/utils.js +832 -0
  156. package/dist/tools/utils.js.map +1 -0
  157. package/dist/types/index.js +8 -0
  158. package/dist/types/index.js.map +1 -0
  159. package/dist/view/components/header/header.js +59 -0
  160. package/dist/view/components/header/header.js.map +1 -0
  161. package/dist/view/components/header/header.less.js +8 -0
  162. package/dist/view/components/header/header.less.js.map +1 -0
  163. package/dist/view/index.js +184 -0
  164. package/dist/view/index.js.map +1 -0
  165. package/dist/view/index.less.js +8 -0
  166. package/dist/view/index.less.js.map +1 -0
  167. package/package.json +33 -6
  168. package/src/agents/app.ts +0 -173
  169. package/src/agents/common.ts +0 -111
  170. package/src/agents/index.ts +0 -7
  171. package/src/components/icons/index.less +0 -8
  172. package/src/components/icons/index.tsx +0 -24
  173. package/src/components/messages/index.less +0 -806
  174. package/src/components/messages/index.tsx +0 -236
  175. package/src/context/index.ts +0 -21
  176. package/src/data.ts +0 -5
  177. package/src/index.tsx +0 -84
  178. package/src/mock.ts +0 -1267
  179. package/src/startView/index.less +0 -216
  180. package/src/startView/index.tsx +0 -229
  181. package/src/tools/analyze-and-expand-prd.ts +0 -166
  182. package/src/tools/answer-user.ts +0 -35
  183. package/src/tools/focus-element.ts +0 -47
  184. package/src/tools/generate-page.ts +0 -750
  185. package/src/tools/get-component-info-by-ids.ts +0 -166
  186. package/src/tools/get-component-info.ts +0 -53
  187. package/src/tools/get-components-doc-and-prd.ts +0 -137
  188. package/src/tools/get-focus-mybricks-dsl.ts +0 -26
  189. package/src/tools/get-mybricks-dsl.ts +0 -73
  190. package/src/tools/index.ts +0 -25
  191. package/src/tools/modify-component.ts +0 -385
  192. package/src/tools/type.d.ts +0 -12
  193. package/src/tools/utils.ts +0 -62
  194. package/src/types.d.ts +0 -65
  195. package/src/view/components/header/header.less +0 -17
  196. package/src/view/components/header/header.tsx +0 -15
  197. package/src/view/components/index.ts +0 -3
  198. package/src/view/components/messages/messages.less +0 -228
  199. package/src/view/components/messages/messages.tsx +0 -172
  200. package/src/view/components/sender/sender.less +0 -44
  201. package/src/view/components/sender/sender.tsx +0 -62
  202. package/src/view/index.less +0 -5
  203. package/src/view/index.tsx +0 -18
@@ -0,0 +1,1076 @@
1
+ import { defineProperty as _defineProperty, asyncToGenerator as _asyncToGenerator, objectSpread2 as _objectSpread2, objectWithoutProperties as _objectWithoutProperties } from '../../../_virtual/_rollupPluginBabelHelpers.js';
2
+ import { getSystemPrompt } from '../prompt/planning.js';
3
+ import { BaseAgent } from './base.js';
4
+ import { parseFileBlocks } from '../tool/util.js';
5
+ import { getToolPrompt } from '../prompt/tool.js';
6
+ import { Events } from '../utils/events.js';
7
+ import { uuid } from '../utils/uuid.js';
8
+ import { RxaiError } from '../error/base.js';
9
+ import { retry } from '../utils/retry.js';
10
+
11
+ var _excluded = ["events"];
12
+ /** 没有response */
13
+ var CATCH_EMPTY = Symbol("CATCH_EMPTY");
14
+ /**
15
+ * 分析计划
16
+ * 执行计划
17
+ */
18
+ class PlanningAgent extends BaseAgent {
19
+ constructor(options) {
20
+ super(options);
21
+ // 设置UUID
22
+ _defineProperty(this, "startTime", 0);
23
+ _defineProperty(this, "endTime", 0);
24
+ _defineProperty(this, "llmContent", "");
25
+ _defineProperty(this, "loading", false);
26
+ _defineProperty(this, "commands", []);
27
+ _defineProperty(this, "filenames", []);
28
+ _defineProperty(this, "defaultPlanList", false);
29
+ /** 事件 */
30
+ _defineProperty(this, "events", new Events());
31
+ /** 整体运行状态 */
32
+ _defineProperty(this, "status", "pending");
33
+ /** error信息 */
34
+ _defineProperty(this, "error", null);
35
+ _defineProperty(this, "enableRetry", true);
36
+ _defineProperty(this, "summaryMessage", "");
37
+ /** 当前请求的cancel */
38
+ _defineProperty(this, "currentRequestCancel", () => {});
39
+ _defineProperty(this, "summaryLoading", false);
40
+ _defineProperty(this, "getHistoryMessages", filenames => {
41
+ return this.options.historyMessages(filenames);
42
+ });
43
+ this.options = options;
44
+ this.uuid = options.uuid || uuid();
45
+ if (options.planList) {
46
+ // 配置默认的规划列表
47
+ var time = new Date().getTime();
48
+ var llmContent = "```bash" + "\n".concat(options.planList.reduce((pre, cur) => {
49
+ return (pre ? pre + " && " : pre) + "node ".concat(cur);
50
+ }, "")) + "\n```";
51
+ this.setStartTime(time);
52
+ this.setEndTime(time);
53
+ this.setLlmContent(llmContent);
54
+ this.setLoading(false);
55
+ this.setCommands(parseBashCommands(llmContent).map(argv => {
56
+ return {
57
+ startTime: 0,
58
+ endTime: 0,
59
+ argv,
60
+ status: null,
61
+ tool: {
62
+ name: argv[1],
63
+ displayName: argv[1]
64
+ },
65
+ content: {
66
+ llm: "",
67
+ display: "",
68
+ response: ""
69
+ }
70
+ };
71
+ }), false);
72
+ this.defaultPlanList = true;
73
+ }
74
+ // 设置userMessage
75
+ this.events.emit("userMessage", this.getUserMessage());
76
+ }
77
+
78
+ /** 随机ID,保证唯一性 */
79
+
80
+ get id() {
81
+ return this.uuid;
82
+ }
83
+
84
+ /** 开始执行 */
85
+ run() {
86
+ var _this = this;
87
+ return _asyncToGenerator(function* () {
88
+ // 记录开始时间
89
+ _this.setStartTime(new Date().getTime());
90
+ yield _this.start();
91
+ })();
92
+ }
93
+ start() {
94
+ var _this2 = this;
95
+ return _asyncToGenerator(function* () {
96
+ _this2.setStatus("pending");
97
+ if (!_this2.commands.length) {
98
+ // 没有commands,需要规划
99
+ _this2.setLoading(true);
100
+ yield _this2.tryCatch(() => retry(() => {
101
+ return _this2.planning();
102
+ }, _this2.options.requestInstance.maxRetries, error => {
103
+ return error instanceof RxaiError && error.type === "request";
104
+ }), true);
105
+ _this2.setLoading(false);
106
+ }
107
+ yield _this2.executeCommands();
108
+
109
+ // 执行结束,调用emits回调通知调用方
110
+ if (_this2.status === "success") {
111
+ _this2.options.emits.complete("");
112
+ } else if (_this2.status === "error") {
113
+ _this2.options.emits.error("");
114
+ }
115
+ _this2.summary();
116
+ })();
117
+ }
118
+
119
+ /** 设置需缓存的值 */
120
+ setStartTime(startTime) {
121
+ this.startTime = startTime;
122
+ this.idbPubContent("startTime", startTime);
123
+ }
124
+ setEndTime(endTime) {
125
+ this.endTime = endTime;
126
+ this.idbPubContent("endTime", endTime);
127
+ }
128
+ setLlmContent(llmContent) {
129
+ this.llmContent = llmContent;
130
+ this.idbPubContent("llmContent", llmContent);
131
+ }
132
+ setLoading(loading) {
133
+ this.loading = loading;
134
+ this.events.emit("loading", loading);
135
+ }
136
+
137
+ /** 构建用户偏好信息提示词 */
138
+ buildGuidePromptSection() {
139
+ return this.options.guidePrompt ? "<\u7528\u6237\u504F\u597D\u4FE1\u606F>\n\u5173\u4E8E\u5F53\u524D\u9879\u76EE\uFF0C\u7528\u6237\u63D0\u4F9B\u4E86\u4ED6\u7684\u504F\u597D\u4FE1\u606F\uFF0C\u8BF7\u6CE8\u610F\u53C2\u8003\u504F\u597D\u4FE1\u606F\u6765\u5B8C\u6210\u4EFB\u52A1\u3002\n".concat(this.options.guidePrompt, "\n</\u7528\u6237\u504F\u597D\u4FE1\u606F>\n") : "";
140
+ }
141
+ setCommands(commands, sync) {
142
+ this.commands = commands.map(command => {
143
+ if (!command.events) {
144
+ command.events = new Events();
145
+ }
146
+ return _objectSpread2({}, command);
147
+ });
148
+ this.events.emit("commands", commands);
149
+ if (sync) {
150
+ this.idbPubContent("commands", commands.map(command => {
151
+ var {
152
+ events
153
+ } = command,
154
+ other = _objectWithoutProperties(command, _excluded);
155
+ return other;
156
+ }));
157
+ }
158
+ }
159
+ idbPubContent(type, content) {
160
+ var _this$error, _this$options$idb;
161
+ // TODO error类型扩展
162
+ if (((_this$error = this.error) === null || _this$error === void 0 ? void 0 : _this$error.message) === "已销毁") {
163
+ return;
164
+ }
165
+ (_this$options$idb = this.options.idb) === null || _this$options$idb === void 0 || _this$options$idb.putContent({
166
+ id: this.uuid,
167
+ type,
168
+ content
169
+ });
170
+ }
171
+
172
+ /** 规划 */
173
+ planning() {
174
+ var _this3 = this;
175
+ return _asyncToGenerator(function* () {
176
+ var {
177
+ options
178
+ } = _this3;
179
+ var planningStream = getPlanningStream(message => {
180
+ _this3.events.emit("planningMessage", message);
181
+ });
182
+ var planningResponse = yield _this3.request({
183
+ messages: yield _this3.getLLMMessages({
184
+ start: [{
185
+ role: "system",
186
+ content: getSystemPrompt({
187
+ title: "MyBricks.ai",
188
+ tools: options.tools,
189
+ prompt: _this3.system.prompt,
190
+ guidePromptSection: _this3.buildGuidePromptSection()
191
+ })
192
+ }, ..._this3.getHistoryMessages()]
193
+ }),
194
+ emits: _this3.getEmits({
195
+ write: chunk => {
196
+ planningStream(chunk);
197
+ }
198
+ })
199
+ });
200
+ if (planningResponse instanceof RxaiError) {
201
+ // 规划出错
202
+ return;
203
+ }
204
+ _this3.setLlmContent(planningResponse);
205
+ var bashCommands = parseBashCommands(planningResponse);
206
+ if (!bashCommands.length) {
207
+ // 说明没有规划
208
+ // this.events.emit("summary", planningResponse);
209
+ _this3.setStatus("success");
210
+ } else {
211
+ var {
212
+ planningCheck
213
+ } = _this3.options;
214
+ if (planningCheck) {
215
+ var check = planningCheck(bashCommands);
216
+ if (!check) {
217
+ throw new RxaiError("规划结果不符合预期", "request");
218
+ }
219
+ bashCommands = check;
220
+ }
221
+ if (bashCommands.length === 1 && bashCommands[0][1] === "get-history-records") {
222
+ bashCommands.push(["node", "analyse-and-answer", {}]);
223
+ }
224
+ _this3.setCommands(bashCommands.map(argv => {
225
+ return {
226
+ startTime: 0,
227
+ endTime: 0,
228
+ argv,
229
+ status: null,
230
+ tool: {
231
+ name: argv[1],
232
+ displayName: argv[1]
233
+ },
234
+ content: {
235
+ llm: "",
236
+ display: "",
237
+ response: ""
238
+ }
239
+ };
240
+ }), true);
241
+ }
242
+ _this3.setEndTime(new Date().getTime());
243
+ })();
244
+ }
245
+
246
+ /** 执行规划的脚本 */
247
+ executeCommands() {
248
+ var _this4 = this;
249
+ return _asyncToGenerator(function* () {
250
+ // 命令
251
+ var commands = _this4.commands;
252
+ if (!commands.length) {
253
+ // 没有命令
254
+ return;
255
+ }
256
+
257
+ // 当前执行到第几个
258
+ var index = commands.findIndex(command => command.status !== "success");
259
+ if (index === -1) {
260
+ // 命令全部执行完成
261
+ return;
262
+ }
263
+ var _loop = function* _loop() {
264
+ var command = commands[index];
265
+
266
+ // 变更状态,开始执行
267
+ command.status = "pending";
268
+ command.startTime = new Date().getTime();
269
+ _this4.setCommands(commands, false);
270
+ var [error, response] = yield _this4.tryCatch(/*#__PURE__*/_asyncToGenerator(function* () {
271
+ return yield retry(() => {
272
+ return _this4.executeCommand(command);
273
+ }, _this4.requestInstance.maxRetries,
274
+ // 暂时都用request配置的maxRetries
275
+ error => {
276
+ return error instanceof RxaiError && error.type === "request";
277
+ });
278
+ }), true);
279
+ if (response === CATCH_EMPTY) {
280
+ command.status = "error";
281
+ if (error instanceof RxaiError && error.type === "tool") {
282
+ Object.assign(command.content, {
283
+ llm: error.message,
284
+ display: error.display
285
+ });
286
+ } else if (error instanceof RxaiError && error.type === "request" || error instanceof Error) {
287
+ var message = error.message;
288
+ Object.assign(command.content, {
289
+ llm: message,
290
+ display: message
291
+ });
292
+ } else {
293
+ var _message = "工具调用错误";
294
+ Object.assign(command.content, {
295
+ llm: _message,
296
+ display: _message
297
+ });
298
+ }
299
+ _this4.setError(error);
300
+ } else {
301
+ command.status = "success";
302
+ Object.assign(command.content, response);
303
+ _this4.setCommands(commands, true);
304
+ }
305
+ command.endTime = new Date().getTime();
306
+ if (_this4.status === "pending") {
307
+ index++;
308
+ // TODO: idb记录状态
309
+ if (commands.length === index) {
310
+ // 所有工具都执行完成,设置完成状态
311
+ _this4.setStatus("success");
312
+
313
+ // this.events.emit(
314
+ // "summary",
315
+ // command.content.display,
316
+ // );
317
+ }
318
+ }
319
+ };
320
+ while (commands.length !== index && _this4.status === "pending") {
321
+ yield* _loop();
322
+ }
323
+ })();
324
+ }
325
+
326
+ /**
327
+ * 执行命令
328
+ * 目前均为node命令,后续可能扩展
329
+ */
330
+ executeCommand(command) {
331
+ var _this5 = this;
332
+ return _asyncToGenerator(function* () {
333
+ var {
334
+ argv
335
+ } = command;
336
+ var [, name, params = {}] = argv;
337
+
338
+ // 已经前置校验过工具合法性,所以tool一定是有的
339
+ var tool = _this5.options.tools.find(tool => {
340
+ return tool.name === name;
341
+ });
342
+ command.tool.displayName = tool.displayName;
343
+
344
+ /** 工具提示词 */
345
+ var toolPrompt = getToolPrompt(tool, {
346
+ attachments: _this5.options.attachments,
347
+ params,
348
+ guidePromptSection: _this5.buildGuidePromptSection()
349
+ });
350
+ var content = {
351
+ llm: "",
352
+ display: "",
353
+ response: ""
354
+ };
355
+ var toolExecute = /*#__PURE__*/function () {
356
+ var _ref2 = _asyncToGenerator(function* (tool, params) {
357
+ var [error, response] = yield _this5.tryCatch(() => {
358
+ if (tool.name === "get-history-records") {
359
+ // @ts-ignore
360
+ _this5.filenames = tool.execute(params);
361
+ return "已读取历史对话记录";
362
+ }
363
+ return tool.execute(params);
364
+ });
365
+ if (response === CATCH_EMPTY) {
366
+ throw error;
367
+ }
368
+ if (typeof response === "string") {
369
+ return {
370
+ llm: response,
371
+ display: response
372
+ };
373
+ } else {
374
+ return {
375
+ llm: response.llmContent,
376
+ display: response.displayContent
377
+ };
378
+ }
379
+ });
380
+ return function toolExecute(_x, _x2) {
381
+ return _ref2.apply(this, arguments);
382
+ };
383
+ }();
384
+ if (!toolPrompt) {
385
+ // 没有提示词,走本地调用,执行execute
386
+ Object.assign(content, yield toolExecute(tool, {
387
+ params,
388
+ files: [],
389
+ content: "",
390
+ replaceContent: "",
391
+ getUserMessage: () => _this5.getUserMessage()
392
+ }));
393
+ } else {
394
+ var _tool$hooks, _this5$options$attach, _tool$aiRole;
395
+ var streamMessage = "";
396
+ var streamError = null;
397
+ var stream = tool.stream ? (content, status) => {
398
+ if (streamError) {
399
+ return;
400
+ }
401
+ if (_this5.status === "error") {
402
+ var _this5$currentRequest;
403
+ (_this5$currentRequest = _this5.currentRequestCancel) === null || _this5$currentRequest === void 0 || _this5$currentRequest.call(_this5);
404
+ streamError = _this5.error;
405
+ return;
406
+ }
407
+ try {
408
+ var {
409
+ content: _replaceContent,
410
+ files: _files
411
+ } = parseFileBlocks(content);
412
+ var res = tool.stream({
413
+ files: _files,
414
+ status,
415
+ replaceContent: _replaceContent
416
+ });
417
+ if (typeof res === "string") {
418
+ var _command$events;
419
+ (_command$events = command.events) === null || _command$events === void 0 || _command$events.emit("streamMessage", {
420
+ message: res,
421
+ status
422
+ });
423
+ }
424
+ } catch (e) {
425
+ var _this5$currentRequest2;
426
+ streamError = e;
427
+ (_this5$currentRequest2 = _this5.currentRequestCancel) === null || _this5$currentRequest2 === void 0 || _this5$currentRequest2.call(_this5);
428
+ }
429
+ } : (content, status) => {
430
+ var _command$events2;
431
+ (_command$events2 = command.events) === null || _command$events2 === void 0 || _command$events2.emit("streamMessage", {
432
+ message: content,
433
+ status
434
+ });
435
+ };
436
+ stream === null || stream === void 0 || stream("", "start");
437
+
438
+ // 执行 before 钩子
439
+ if ((_tool$hooks = tool.hooks) !== null && _tool$hooks !== void 0 && _tool$hooks.before && typeof tool.hooks.before === "function") {
440
+ yield tool.hooks.before({
441
+ params
442
+ });
443
+ }
444
+ var historyMessages = _this5.getHistoryMessages(_this5.filenames);
445
+ var llmMessages = yield _this5.getLLMMessages({
446
+ start: [{
447
+ role: "system",
448
+ content: getToolPrompt(tool, {
449
+ attachments: _this5.options.attachments,
450
+ params,
451
+ guidePromptSection: _this5.buildGuidePromptSection()
452
+ })
453
+ }, ...historyMessages]
454
+ });
455
+
456
+ // 检查是否有附件:当前请求的附件 + 历史记录中带进来的附件
457
+ // 只要 content 是数组结构就说明包含了附件
458
+ var hasHistoryAttachments = historyMessages.some(msg => {
459
+ return Array.isArray(msg.content);
460
+ });
461
+ var hasAttachments = !!((_this5$options$attach = _this5.options.attachments) !== null && _this5$options$attach !== void 0 && _this5$options$attach.length || hasHistoryAttachments);
462
+ var response = yield _this5.request({
463
+ messages: llmMessages,
464
+ emits: _this5.getEmits({
465
+ write: chunk => {
466
+ // if (tool.streamThoughts) {
467
+ // this.events.emit("streamMessage", chunk);
468
+ // }
469
+
470
+ streamMessage += chunk;
471
+ command.content.response = streamMessage;
472
+ stream === null || stream === void 0 || stream(streamMessage, "ing");
473
+ // if (!stream) {
474
+ // command.events?.emit("streamMessage", {
475
+ // message: streamMessage,
476
+ // status: "ing",
477
+ // });
478
+ // this.events.emit("streamMessage", chunk);
479
+ // }
480
+ },
481
+ complete: content => {
482
+ stream === null || stream === void 0 || stream(content, "complete");
483
+ }
484
+ }),
485
+ aiRole: typeof tool.aiRole === "function" ? (_tool$aiRole = tool.aiRole) === null || _tool$aiRole === void 0 ? void 0 : _tool$aiRole.call(tool, {
486
+ params,
487
+ hasAttachments
488
+ }) : tool.aiRole
489
+ });
490
+ if (streamError) {
491
+ throw streamError;
492
+ }
493
+ if (response instanceof RxaiError) {
494
+ throw response;
495
+ }
496
+
497
+ // 解析文件
498
+ var {
499
+ files,
500
+ content: replaceContent
501
+ } = parseFileBlocks(response);
502
+ Object.assign(content, yield toolExecute(tool, {
503
+ params,
504
+ files,
505
+ content: response,
506
+ replaceContent,
507
+ getUserMessage: () => _this5.getUserMessage()
508
+ }), {
509
+ response
510
+ });
511
+ }
512
+ return content;
513
+ })();
514
+ }
515
+
516
+ /** emits代理 */
517
+ getEmits(emits) {
518
+ var {
519
+ options
520
+ } = this;
521
+ return {
522
+ write: chunk => {
523
+ var _emits$write;
524
+ options.emits.write(chunk);
525
+ emits === null || emits === void 0 || (_emits$write = emits.write) === null || _emits$write === void 0 || _emits$write.call(emits, chunk);
526
+ },
527
+ complete: content => {
528
+ var _emits$complete;
529
+ emits === null || emits === void 0 || (_emits$complete = emits.complete) === null || _emits$complete === void 0 || _emits$complete.call(emits, content);
530
+ },
531
+ error: _error => {
532
+ var _emits$error;
533
+ options.emits.error(_error);
534
+ // this.setStatus("error");
535
+ console.error(_error);
536
+ emits === null || emits === void 0 || (_emits$error = emits.error) === null || _emits$error === void 0 || _emits$error.call(emits, _error);
537
+ },
538
+ cancel: fn => {
539
+ var _emits$cancel;
540
+ options.emits.cancel(fn);
541
+ emits === null || emits === void 0 || (_emits$cancel = emits.cancel) === null || _emits$cancel === void 0 || _emits$cancel.call(emits, fn);
542
+ this.currentRequestCancel = fn;
543
+ }
544
+ };
545
+ }
546
+
547
+ /** 获取用户需求 */
548
+ getUserMessage() {
549
+ var _options$attachments;
550
+ var {
551
+ options
552
+ } = this;
553
+ return {
554
+ role: "user",
555
+ content: (_options$attachments = options.attachments) !== null && _options$attachments !== void 0 && _options$attachments.length ? [{
556
+ type: "text",
557
+ text: options.message
558
+ }, ...options.attachments.filter(attachement => {
559
+ return attachement.type === "image";
560
+ }).map(attachement => {
561
+ return {
562
+ type: "image_url",
563
+ image_url: {
564
+ url: attachement.content
565
+ }
566
+ };
567
+ })] : options.message
568
+ };
569
+ }
570
+ formatUserMessage(userMessage, formatFunction) {
571
+ var userTextMessage;
572
+ var newUserMessage;
573
+
574
+ // 提取用户消息内容并创建新的消息对象
575
+ if (typeof (userMessage === null || userMessage === void 0 ? void 0 : userMessage.content) === "string") {
576
+ newUserMessage = _objectSpread2({}, userMessage);
577
+ userTextMessage = userMessage.content;
578
+ } else if (Array.isArray(userMessage === null || userMessage === void 0 ? void 0 : userMessage.content)) {
579
+ var _userMessage$content$;
580
+ newUserMessage = _objectSpread2(_objectSpread2({}, userMessage), {}, {
581
+ content: [...userMessage.content]
582
+ });
583
+ var idx = userMessage.content.findIndex(item => item.type === "text");
584
+ userTextMessage = (_userMessage$content$ = userMessage.content[idx]) === null || _userMessage$content$ === void 0 ? void 0 : _userMessage$content$.text;
585
+ }
586
+
587
+ // 通过format函数处理文本内容
588
+ var formattedText = formatFunction(userTextMessage);
589
+
590
+ // 将格式化后的内容塞回新消息对象
591
+ if (typeof (userMessage === null || userMessage === void 0 ? void 0 : userMessage.content) === "string") {
592
+ newUserMessage.content = formattedText;
593
+ } else if (Array.isArray(userMessage === null || userMessage === void 0 ? void 0 : userMessage.content)) {
594
+ var _idx = userMessage.content.findIndex(item => item.type === "text");
595
+ if (_idx !== -1) {
596
+ newUserMessage.content[_idx].text = formattedText;
597
+ }
598
+ }
599
+ return newUserMessage;
600
+ }
601
+
602
+ /** 获取对话消息列表 */
603
+ getLLMMessages(params) {
604
+ var _this6 = this;
605
+ return _asyncToGenerator(function* () {
606
+ var {
607
+ options
608
+ } = _this6;
609
+ var {
610
+ start,
611
+ end
612
+ } = params;
613
+ // 辅助函数,用于从 argv 构建命令字符串
614
+ var buildCommandString = argv => {
615
+ var [bash, name, params = {}] = argv;
616
+ return "".concat(name, " ").concat(Object.entries(params).reduce((acc, _ref3) => {
617
+ var [key, value] = _ref3;
618
+ return acc + "-".concat(key, " ").concat(value, " ");
619
+ }, "")).trim();
620
+ };
621
+
622
+ // 获取用户原始需求
623
+ var userMessage = typeof options.formatUserMessage === "function" ? _this6.formatUserMessage(_this6.getUserMessage(), options.formatUserMessage) : _this6.getUserMessage();
624
+
625
+ // 如果存在命令,则构建“工具规划”和“执行进度”
626
+ if (_this6.commands.length > 0) {
627
+ // 预先找到当前正在执行的命令
628
+ var currentCommand = _this6.commands.find(c => c.status === "pending");
629
+ var currentCommandString = currentCommand ? buildCommandString(currentCommand.argv) : "无";
630
+
631
+ // --- 构建“工具规划”部分 ---
632
+ var planningContent = "\n\n---\n## \u5DE5\u5177\u89C4\u5212";
633
+ planningContent += "\n\u4E3A\u4E86\u5E2E\u52A9\u7528\u6237\u8FBE\u6210\u4E0A\u8FF0\u76EE\u7684\uFF0C\u7CFB\u7EDF\u89C4\u5212\u4E86\u4EE5\u4E0B\u5DE5\u5177\u6765\u5904\u7406\uFF0C\u5F53\u524D\u6B63\u5728\u6267\u884C ".concat(currentCommandString, "\u3002");
634
+ for (var command of _this6.commands) {
635
+ if (command.status === null) continue;
636
+ var statusMap = {
637
+ success: "[已完成]",
638
+ pending: "[正在执行]"
639
+ };
640
+ var status = statusMap[command.status] || "[待执行]";
641
+ planningContent += "\n".concat(status, " ").concat(buildCommandString(command.argv));
642
+ }
643
+
644
+ // --- 构建“执行进度”部分 ---
645
+ var progressContent = "\n\n## \u6267\u884C\u8FDB\u5EA6";
646
+ for (var _command of _this6.commands) {
647
+ if (_command.status === null) continue;
648
+ var commandStr = buildCommandString(_command.argv);
649
+ switch (_command.status) {
650
+ case "success":
651
+ {
652
+ progressContent += "\n\n[\u5DF2\u5B8C\u6210] ".concat(commandStr);
653
+ var output = _command.content.llm || _command.content.display;
654
+ progressContent += "\n----- \u8F93\u51FA\u5185\u5BB9 -----\n".concat(output, "\n----- \u8F93\u51FA\u5185\u5BB9 -----");
655
+ break;
656
+ }
657
+ case "pending":
658
+ progressContent += "\n\n[\u6B63\u5728\u6267\u884C] ".concat(commandStr);
659
+ if (_this6.error instanceof RxaiError && _this6.error.type === "tool") {
660
+ progressContent += "\n\u6267\u884C\u65F6\u51FA\u9519: ".concat(_this6.error.message, "\n\u8BF7\u5206\u6790\u9519\u8BEF\u539F\u56E0\uFF0C\u4FEE\u6B63\u4E0A\u8FF0\u547D\u4EE4\u6216\u91CD\u65B0\u89C4\u5212\u3002");
661
+ } else {
662
+ progressContent += "\n请根据工具描述、用户消息、以及前置工具的执行结果,为当前步骤提供输出。";
663
+ }
664
+ break;
665
+ default:
666
+ progressContent += "\n\n[\u5F85\u6267\u884C] ".concat(commandStr);
667
+ break;
668
+ }
669
+ }
670
+ // 组合所有内容
671
+ userMessage = _this6.formatUserMessage(userMessage, msg => msg + planningContent + progressContent);
672
+ }
673
+
674
+ // 附加重试错误信息
675
+ var retryMessage = [];
676
+ if (_this6.error instanceof RxaiError && _this6.error.type === "retry") {
677
+ retryMessage.push({
678
+ role: "user",
679
+ // @ts-ignore
680
+ content: "[\u7CFB\u7EDF\u63D0\u793A] \u4E0A\u6B21\u89C4\u5212\u51FA\u9519\uFF0C\u9519\u8BEF\u4FE1\u606F: ".concat(_this6.error.message, "\u3002\u8BF7\u57FA\u4E8E\u7528\u6237\u9700\u6C42\u91CD\u65B0\u89C4\u5212\u3002")
681
+ });
682
+ }
683
+ var guideMessage = [];
684
+
685
+ // 组装最终消息列表
686
+ var presetMsgs = typeof options.presetMessages === "function" ? yield options.presetMessages() : options.presetMessages;
687
+ var messages = [...presetMsgs, ...guideMessage, userMessage, ...retryMessage];
688
+ if (start) {
689
+ messages.unshift(...start);
690
+ }
691
+ if (end) {
692
+ messages.push(...end);
693
+ }
694
+ _this6.setError(null);
695
+ return messages;
696
+ })();
697
+ }
698
+
699
+ /** 请求统一封装 */
700
+ request(params) {
701
+ var _this7 = this;
702
+ return _asyncToGenerator(function* () {
703
+ var response = yield _this7.requestInstance.requestAsStream(_objectSpread2(_objectSpread2({}, params), {}, {
704
+ enableLog: _this7.enableLog
705
+ }));
706
+ if (response.type === "error") {
707
+ _this7.setError(response.content);
708
+ return response.content;
709
+ } else if (response.type === "cancel") {
710
+ var error = new RxaiError("已取消执行", "request");
711
+ _this7.setError(error);
712
+ return error;
713
+ }
714
+ return response.content;
715
+ })();
716
+ }
717
+
718
+ /** 统一错误处理 */
719
+ setError(error) {
720
+ if (!error) {
721
+ // error清理
722
+ this.error = null;
723
+ this.events.emit("error", "");
724
+ this.idbPubContent("error", null);
725
+ return;
726
+ }
727
+ this.setStatus("error");
728
+ if (error instanceof RxaiError) {
729
+ this.error = error;
730
+ } else {
731
+ // 默认为ToolError
732
+ this.error = new RxaiError((error === null || error === void 0 ? void 0 : error.message) || "工具调用错误", "tool");
733
+ }
734
+ this.idbPubContent("error", this.error.toJSON());
735
+ this.events.emit("error", this.error.display);
736
+ }
737
+
738
+ /** 设置状态 */
739
+ setStatus(status) {
740
+ this.status = status;
741
+ this.idbPubContent("status", status);
742
+ this.events.emit("status", status);
743
+ }
744
+
745
+ /** TODO: 获取DB存储的plan静态数据 */
746
+ getDBContent() {
747
+ var _this8 = this;
748
+ return _asyncToGenerator(function* () {
749
+ var {
750
+ options
751
+ } = _this8;
752
+ var presetMsgs = typeof options.presetMessages === "function" ? yield options.presetMessages() : options.presetMessages;
753
+ return {
754
+ uuid: _this8.uuid,
755
+ extension: options.extension,
756
+ enableLog: options.enableLog,
757
+ attachments: options.attachments,
758
+ message: options.message,
759
+ presetHistoryMessages: options.presetHistoryMessages,
760
+ presetMessages: presetMsgs,
761
+ planList: options.planList,
762
+ blockId: options.blockId
763
+ };
764
+ })();
765
+ }
766
+
767
+ /** TODO: 从DB恢复 */
768
+ recover(params) {
769
+ this.enableRetry = false;
770
+ params.forEach(_ref4 => {
771
+ var {
772
+ type,
773
+ content
774
+ } = _ref4;
775
+ if (type === "error") {
776
+ if (content) {
777
+ this.error = new RxaiError(content.message, content.type, content.display).recover(content);
778
+ }
779
+ } else {
780
+ // @ts-ignore
781
+ this[type] = content;
782
+ }
783
+ });
784
+ if (this.status === "pending") {
785
+ // 未正常完成,设置为取消状态
786
+ this.setStatus("aborted");
787
+ }
788
+ this.setLoading(false);
789
+ var commands = this.commands;
790
+ commands.forEach(command => {
791
+ if (command.status === "pending") {
792
+ command.status = null;
793
+ }
794
+ });
795
+ this.setCommands(commands, false);
796
+ if (this.error) {
797
+ this.events.emit("error", this.error.display);
798
+ } else {
799
+ if (this.status === "aborted") {
800
+ this.events.emit("summary", "已取消");
801
+ } else if (!commands.length) {
802
+ this.events.emit("summary", this.llmContent);
803
+ }
804
+ }
805
+ if (commands.length) {
806
+ var bashIndex = this.llmContent.indexOf("```bash");
807
+ this.events.emit("planningMessage", this.llmContent.slice(0, bashIndex));
808
+ }
809
+ }
810
+
811
+ /** 获取扩展参数 */
812
+ get extension() {
813
+ return this.options.extension;
814
+ }
815
+ get blockId() {
816
+ return this.options.blockId;
817
+ }
818
+
819
+ /** TODO: 获取当前plan的总结信息 */
820
+ getMessages() {
821
+ var _this$error2;
822
+ if (this.loading || this.status === "pending" || this.messages.length) {
823
+ return null;
824
+ }
825
+
826
+ // if (!this.summaryMessage) {
827
+ // this.summary();
828
+ // }
829
+ var message = "";
830
+ var presetHistoryMessages = this.options.presetHistoryMessages;
831
+ if (presetHistoryMessages !== null && presetHistoryMessages !== void 0 && presetHistoryMessages.length) {
832
+ message += "### 系统信息" + "".concat(presetHistoryMessages.reduce((pre, cur) => {
833
+ return pre + "\n".concat(cur.content);
834
+ }, ""));
835
+ }
836
+ message += "\n\n### 用户消息" + "\n".concat(this.options.message);
837
+ if (this.commands.length) {
838
+ message += "\n\n### 工具调用记录" + "".concat(this.commands.reduce((pre, command, index) => {
839
+ if (!command.status) {
840
+ return pre;
841
+ }
842
+
843
+ // const isLast = this.commands.length - 1 === index;
844
+
845
+ var [bash, name, params = {}] = command.argv;
846
+ if (command.status === "success") {
847
+ return pre + "\n- [x] ".concat(bash, " ").concat(name, " ").concat(Object.entries(params).reduce((pre, _ref5) => {
848
+ var [key, value] = _ref5;
849
+ return pre + "-".concat(key, " ").concat(value, " ");
850
+ }, "")) + // (isLast
851
+ // ? `\n${command.content.llm || command.content.display}`
852
+ // : "")
853
+ "\nstdout\uFF1A".concat(command.content.llm || command.content.display);
854
+ } else if (command.status === "error") {
855
+ return pre + "\n- [] ".concat(bash, " ").concat(name, " ").concat(Object.entries(params).reduce((pre, _ref6) => {
856
+ var [key, value] = _ref6;
857
+ return pre + "-".concat(key, " ").concat(value, " ");
858
+ }, "")) + "\nzsh\uFF1A".concat(command.content.llm || command.content.display);
859
+ }
860
+ return pre;
861
+ }, ""));
862
+ }
863
+ message += "\n\n### 状态" + (this.status === "success" ? "\nsuccess".concat(!this.commands.length ? "\uFF1A".concat(this.llmContent) : "") : "") + (this.status === "aborted" ? "\naborted:执行中断" : "") + (this.status === "error" ? "\nerror\uFF1A".concat((_this$error2 = this.error) === null || _this$error2 === void 0 ? void 0 : _this$error2.message) : "");
864
+ return {
865
+ message,
866
+ summaryMessage: this.summaryMessage ? "".concat(presetHistoryMessages !== null && presetHistoryMessages !== void 0 && presetHistoryMessages.length ? presetHistoryMessages.reduce((pre, cur) => {
867
+ return pre + "\n".concat(cur.content);
868
+ }, "") + "\n" : "") + this.summaryMessage : "",
869
+ attachments: this.options.attachments
870
+ };
871
+ }
872
+
873
+ /** 安全执行 */
874
+ tryCatch(task) {
875
+ var _arguments = arguments,
876
+ _this9 = this;
877
+ return _asyncToGenerator(function* () {
878
+ var log = _arguments.length > 1 && _arguments[1] !== undefined ? _arguments[1] : false;
879
+ try {
880
+ var result = task();
881
+ if (result instanceof Promise) {
882
+ return [undefined, yield result];
883
+ }
884
+ return [undefined, result];
885
+ } catch (e) {
886
+ var _this9$error;
887
+ // TODO error类型扩展
888
+ if (log && ((_this9$error = _this9.error) === null || _this9$error === void 0 ? void 0 : _this9$error.message) !== "已销毁") {
889
+ console.error("[Rxai - planning - error]", e);
890
+ }
891
+ return [e, CATCH_EMPTY];
892
+ }
893
+ })();
894
+ }
895
+ retry() {
896
+ var _this0 = this;
897
+ return _asyncToGenerator(function* () {
898
+ if (_this0.error instanceof RxaiError && _this0.error.type === "retry") {
899
+ // 从意图识别开始
900
+ if (_this0.defaultPlanList) {
901
+ // 有默认配置,重制commands
902
+ _this0.setCommands(parseBashCommands(_this0.llmContent).map(argv => {
903
+ return {
904
+ startTime: 0,
905
+ endTime: 0,
906
+ argv,
907
+ status: null,
908
+ tool: {
909
+ name: argv[1],
910
+ displayName: argv[1]
911
+ },
912
+ content: {
913
+ llm: "",
914
+ display: "",
915
+ response: ""
916
+ }
917
+ };
918
+ }), true);
919
+ } else {
920
+ // 清空规划
921
+ _this0.setLlmContent("");
922
+ _this0.setCommands([], true);
923
+ }
924
+
925
+ // this.setError(null);
926
+ }
927
+ yield _this0.start();
928
+ })();
929
+ }
930
+ export() {
931
+ var _this1 = this;
932
+ return _asyncToGenerator(function* () {
933
+ var {
934
+ options
935
+ } = _this1;
936
+ var presetMsgs = typeof options.presetMessages === "function" ? yield options.presetMessages() : options.presetMessages;
937
+ return {
938
+ uuid: _this1.uuid,
939
+ options: {
940
+ attachments: options.attachments,
941
+ extension: options.extension,
942
+ message: options.message,
943
+ presetHistoryMessages: options.presetHistoryMessages,
944
+ presetMessages: presetMsgs
945
+ },
946
+ commands: _this1.commands,
947
+ defaultPlanList: _this1.defaultPlanList,
948
+ enableLog: _this1.enableLog,
949
+ enableRetry: _this1.enableRetry,
950
+ endTime: _this1.endTime,
951
+ error: _this1.error ? _this1.error.toJSON() : undefined,
952
+ llmContent: _this1.llmContent,
953
+ startTime: _this1.startTime,
954
+ status: _this1.status,
955
+ summaryMessage: _this1.summaryMessage
956
+ };
957
+ })();
958
+ }
959
+ summary() {
960
+ if (this.summaryLoading) {
961
+ return;
962
+ }
963
+ if (this.summaryMessage) {
964
+ return;
965
+ }
966
+ this.summaryLoading = true;
967
+ var historyMessage = this.getMessages();
968
+ if (!historyMessage) {
969
+ return;
970
+ }
971
+ var {
972
+ message
973
+ } = historyMessage;
974
+ this.requestInstance.requestAsStream({
975
+ messages: [{
976
+ role: "system",
977
+ content: "\u8BF7\u5BF9\u4EE5\u4E0B\u5BF9\u8BDD\u5386\u53F2\u8BB0\u5F55\u8FDB\u884C**\u4E13\u4E1A\u6458\u8981**\uFF0C\u63D0\u53D6\u6838\u5FC3\u64CD\u4F5C\u4E0E\u7ED3\u679C\uFF0C\u8BED\u8A00\u7CBE\u70BC\uFF0C\u9002\u5408\u5B58\u6863\u548C\u5FEB\u901F\u67E5\u9605\u3002\n\n---\n\n**\u6458\u8981\u8981\u6C42\uFF1A**\n1. **\u7528\u6237\u610F\u56FE**\uFF1A\u7528\u4E00\u53E5\u8BDD\u603B\u7ED3\u7528\u6237\u7684\u539F\u59CB\u6307\u4EE4\u6216\u8BF7\u6C42\u3002\n2. **\u5173\u952E\u64CD\u4F5C**\uFF1A\u5217\u51FA\u6267\u884C\u7684\u4E3B\u8981\u52A8\u4F5C\uFF08\u5DE5\u5177\u8C03\u7528\u548C\u91CD\u8981\u4FEE\u6539\uFF09\u3002\n3. **\u6267\u884C\u7ED3\u679C**\uFF1A\u6982\u62EC\u4EFB\u52A1\u6267\u884C\u7684\u5173\u952E\u4EA7\u51FA\u6216\u53D8\u5316\u3002\n4. **\u6700\u7EC8\u72B6\u6001**\uFF1A\u8BF4\u660E\u4EFB\u52A1\u5B8C\u6210\u60C5\u51B5\u3002\n\n---\n\n**\u8F93\u51FA\u683C\u5F0F\u793A\u4F8B\uFF1A**\n```\n\u3010\u7528\u6237\u610F\u56FE\u3011\u7528\u6237\u5E0C\u671B\u5B9E\u73B0\u67D0\u4E2A\u5177\u4F53\u76EE\u6807\u3002\n\u3010\u5173\u952E\u64CD\u4F5C\u3011\u8C03\u7528\u4E86\u5DE5\u5177A\u548CB\u3002\n\u3010\u6267\u884C\u7ED3\u679C\u3011\u4EA7\u751F\u4E86Z\u53D8\u5316\u6216\u8FBE\u6210\u4E86W\u6548\u679C\u3002\n\u3010\u6700\u7EC8\u72B6\u6001\u3011\u6210\u529F\u5B8C\u6210/\u90E8\u5206\u5B8C\u6210/\u5931\u8D25\u3002\n```\n\n---\n\n**\u6CE8\u610F\uFF1A**\n1. \u6458\u8981\u7ED3\u679C\u5FC5\u987B\u7CBE\u7B80\uFF0C\u603B\u5B57\u6570\u4E0D\u8D85\u8FC750\u5B57\u3002\n\n---\n\n\u8BF7\u6839\u636E\u4EE5\u4E0A\u8981\u6C42\uFF0C\u76F4\u63A5\u7ED9\u51FA\u6E05\u6670\u3001\u7B80\u660E\u7684\u6458\u8981\u7ED3\u679C\u3002"
978
+ }, {
979
+ role: "user",
980
+ content: "<\u5BF9\u8BDD\u5386\u53F2\u8BB0\u5F55>\n".concat(message, "\n</\u5BF9\u8BDD\u5386\u53F2\u8BB0\u5F55>")
981
+ }],
982
+ emits: {
983
+ write() {},
984
+ cancel() {},
985
+ error() {},
986
+ complete() {}
987
+ }
988
+ }).then(res => {
989
+ if (res.type === "complete") {
990
+ this.summaryMessage = res.content;
991
+ this.idbPubContent("summaryMessage", res.content);
992
+ }
993
+ }).catch(e => {
994
+ console.error(e);
995
+ }).finally(() => {
996
+ this.summaryLoading = false;
997
+ });
998
+ return {
999
+ message,
1000
+ attachments: this.options.attachments
1001
+ };
1002
+ }
1003
+ destroy() {
1004
+ var _this$currentRequestC;
1005
+ this.status = "error";
1006
+ this.error = new RxaiError("已销毁", "tool");
1007
+ (_this$currentRequestC = this.currentRequestCancel) === null || _this$currentRequestC === void 0 || _this$currentRequestC.call(this);
1008
+ }
1009
+ }
1010
+ function parseBashCommands(string) {
1011
+ var bashCodeRegex = /```\s*bash([\s\S]*?)```/;
1012
+ var matchResult = string.match(bashCodeRegex);
1013
+ if (!matchResult) return [];
1014
+ var bashContent = matchResult[1].trim();
1015
+ var subCommands = bashContent.split("&&").map(cmd => cmd.trim());
1016
+ var commandArray = subCommands.map(cmd => cmd.split(/\s+/).filter(Boolean));
1017
+ var result = [];
1018
+ commandArray.forEach(command => {
1019
+ var [node, filename, ...args] = command;
1020
+ var params = {};
1021
+ var key = "";
1022
+ args.forEach(arg => {
1023
+ if (arg.startsWith("-")) {
1024
+ // 支持 -v 或 --option 格式,移除前面的 - 或 --
1025
+ key = arg.replace(/^--?/, "");
1026
+ } else if (key) {
1027
+ // 当前面有key时,当前值作为参数值
1028
+ params[key] = arg;
1029
+ key = ""; // 重置key,准备接收下一个参数
1030
+ }
1031
+ });
1032
+ result.push([node, filename, params]);
1033
+ });
1034
+ return result;
1035
+ }
1036
+ function getPlanningStream(write) {
1037
+ var stopWrite = false;
1038
+ var planningMessage = "";
1039
+ var temp = "";
1040
+ return chunk => {
1041
+ if (stopWrite) {
1042
+ return;
1043
+ }
1044
+ var tempChunk = temp + chunk;
1045
+ var backticksIndex = tempChunk.indexOf("`");
1046
+ if (temp && tempChunk.length >= 7) {
1047
+ if (new RegExp("^".concat(tempChunk.slice(0, 7))).test("```bash")) {
1048
+ stopWrite = true;
1049
+ } else {
1050
+ temp = "";
1051
+ planningMessage += tempChunk;
1052
+ }
1053
+ } else {
1054
+ if (backticksIndex !== -1) {
1055
+ if (tempChunk.slice(backticksIndex, backticksIndex + 7) === "```bash") {
1056
+ stopWrite = true;
1057
+ planningMessage += tempChunk.slice(0, backticksIndex);
1058
+ } else {
1059
+ planningMessage += tempChunk.slice(0, backticksIndex);
1060
+ temp = tempChunk.slice(backticksIndex);
1061
+ }
1062
+ } else {
1063
+ planningMessage += chunk;
1064
+ }
1065
+ }
1066
+ var bashIndex = planningMessage.indexOf("```bash");
1067
+ if (bashIndex !== -1) {
1068
+ stopWrite = true;
1069
+ planningMessage = planningMessage.slice(0, bashIndex);
1070
+ }
1071
+ write(planningMessage);
1072
+ };
1073
+ }
1074
+
1075
+ export { PlanningAgent };
1076
+ //# sourceMappingURL=planning.js.map