@pulse-editor/cli 0.1.4 ā 0.1.5
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.
- package/dist/lib/server/express.js +8 -2
- package/dist/lib/webpack/configs/mf-server.js +4 -252
- package/dist/lib/webpack/configs/preview.d.ts +1 -0
- package/dist/lib/webpack/configs/preview.js +124 -3
- package/dist/lib/webpack/configs/utils.d.ts +11 -0
- package/dist/lib/webpack/configs/utils.js +221 -2
- package/dist/lib/webpack/webpack-config.js +3 -3
- package/package.json +1 -1
|
@@ -97,8 +97,14 @@ app.all(/^\/server-function\/(.*)/, async (req, res) => {
|
|
|
97
97
|
});
|
|
98
98
|
if (isPreview) {
|
|
99
99
|
/* Preview mode */
|
|
100
|
-
app.get("/pulse.config.json", (_req, res) => {
|
|
101
|
-
|
|
100
|
+
app.get("/pulse.config.json", async (_req, res) => {
|
|
101
|
+
try {
|
|
102
|
+
const data = await import("fs/promises").then((fs) => fs.readFile("dist/pulse.config.json", "utf-8"));
|
|
103
|
+
res.type("json").send(data);
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
res.status(404).json({ error: "pulse.config.json not found" });
|
|
107
|
+
}
|
|
102
108
|
});
|
|
103
109
|
app.use(express.static("dist/client"));
|
|
104
110
|
// Expose skill actions as REST API endpoints in dev and preview modes
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
import mfNode from "@module-federation/node";
|
|
3
3
|
import fs from "fs";
|
|
4
|
-
import { globSync } from "glob";
|
|
5
4
|
import path from "path";
|
|
6
|
-
import { Node, Project, SyntaxKind } from "ts-morph";
|
|
7
5
|
import wp from "webpack";
|
|
8
|
-
import { discoverAppSkillActions, discoverServerFunctions, loadPulseConfig, } from "./utils.js";
|
|
6
|
+
import { compileAppActionSkills, discoverAppSkillActions, discoverServerFunctions, loadPulseConfig, } from "./utils.js";
|
|
9
7
|
const { NodeFederationPlugin } = mfNode;
|
|
10
8
|
const { webpack } = wp;
|
|
11
9
|
class MFServerPlugin {
|
|
@@ -48,13 +46,13 @@ class MFServerPlugin {
|
|
|
48
46
|
: false;
|
|
49
47
|
if (isActionChange) {
|
|
50
48
|
console.log(`[Server] Detected changes in actions. Recompiling...`);
|
|
51
|
-
this.
|
|
49
|
+
compileAppActionSkills(this.pulseConfig);
|
|
52
50
|
}
|
|
53
51
|
}
|
|
54
52
|
else {
|
|
55
53
|
console.log(`[Server] š Building app...`);
|
|
56
54
|
await this.compileServerFunctions(compiler);
|
|
57
|
-
this.
|
|
55
|
+
compileAppActionSkills(this.pulseConfig);
|
|
58
56
|
console.log(`[Server] ā
Successfully built server.`);
|
|
59
57
|
const funcs = discoverServerFunctions();
|
|
60
58
|
console.log(`\nš Server functions:
|
|
@@ -85,7 +83,7 @@ ${Object.entries(funcs)
|
|
|
85
83
|
else {
|
|
86
84
|
try {
|
|
87
85
|
await this.compileServerFunctions(compiler);
|
|
88
|
-
this.
|
|
86
|
+
compileAppActionSkills(this.pulseConfig);
|
|
89
87
|
}
|
|
90
88
|
catch (err) {
|
|
91
89
|
console.error(`[Server] ā Error during compilation:`, err);
|
|
@@ -158,252 +156,6 @@ ${Object.entries(funcs)
|
|
|
158
156
|
},
|
|
159
157
|
}, {});
|
|
160
158
|
}
|
|
161
|
-
/**
|
|
162
|
-
* Register default functions defined in src/skill as exposed modules in Module Federation.
|
|
163
|
-
* This will:
|
|
164
|
-
* 1. Search for all .ts files under src/skill
|
|
165
|
-
* 2. Use ts-morph to get the default function information, including function name, parameters, and JSDoc comments
|
|
166
|
-
* 3. Organize the functions' information into a list of Action
|
|
167
|
-
* @param compiler
|
|
168
|
-
*/
|
|
169
|
-
compileAppActionSkills() {
|
|
170
|
-
// 1. Get all TypeScript files under src/skill
|
|
171
|
-
const files = globSync("./src/skill/*/action.ts");
|
|
172
|
-
const project = new Project({
|
|
173
|
-
tsConfigFilePath: path.join(process.cwd(), "node_modules/.pulse/tsconfig.server.json"),
|
|
174
|
-
});
|
|
175
|
-
const actions = [];
|
|
176
|
-
files.forEach((file) => {
|
|
177
|
-
const sourceFile = project.addSourceFileAtPath(file);
|
|
178
|
-
const defaultExportSymbol = sourceFile.getDefaultExportSymbol();
|
|
179
|
-
if (!defaultExportSymbol)
|
|
180
|
-
return;
|
|
181
|
-
const defaultExportDeclarations = defaultExportSymbol.getDeclarations();
|
|
182
|
-
defaultExportDeclarations.forEach((declaration) => {
|
|
183
|
-
if (declaration.getKind() !== SyntaxKind.FunctionDeclaration)
|
|
184
|
-
return;
|
|
185
|
-
const funcDecl = declaration.asKindOrThrow(SyntaxKind.FunctionDeclaration);
|
|
186
|
-
// Get action name from path `src/skill/{actionName}/action.ts`
|
|
187
|
-
// Match `*/src/skill/{actionName}/action.ts` and extract {actionName}
|
|
188
|
-
const pattern = /src\/skill\/([^\/]+)\/action\.ts$/;
|
|
189
|
-
const match = file.replaceAll("\\", "/").match(pattern);
|
|
190
|
-
if (!match) {
|
|
191
|
-
console.warn(`File path ${file} does not match pattern ${pattern}. Skipping...`);
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
const actionName = match[1];
|
|
195
|
-
if (!actionName) {
|
|
196
|
-
console.warn(`Could not extract action name from file path ${file}. Skipping...`);
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
// Throw an error if the funcName is duplicated with an existing action to prevent accidental overwriting
|
|
200
|
-
if (actions.some((action) => action.name === actionName)) {
|
|
201
|
-
throw new Error(`Duplicate action name "${actionName}" detected in file ${file}. Please ensure all actions have unique names to avoid conflicts.`);
|
|
202
|
-
}
|
|
203
|
-
const defaultExportJSDocs = funcDecl.getJsDocs();
|
|
204
|
-
// Validate that the function has a JSDoc description
|
|
205
|
-
const descriptionText = defaultExportJSDocs
|
|
206
|
-
.map((doc) => doc.getDescription().replace(/^\*+/gm, "").trim())
|
|
207
|
-
.join("\n")
|
|
208
|
-
.trim();
|
|
209
|
-
if (defaultExportJSDocs.length === 0 || !descriptionText) {
|
|
210
|
-
throw new Error(`[Action Validation] Action "${actionName}" in ${file} is missing a JSDoc description. ` +
|
|
211
|
-
`Please add a JSDoc comment block with a description above the function.` +
|
|
212
|
-
`Run \`pulse skill fix ${actionName}\` to automatically add a JSDoc template for this action.`);
|
|
213
|
-
}
|
|
214
|
-
const description = defaultExportJSDocs
|
|
215
|
-
.map((doc) => doc.getFullText())
|
|
216
|
-
.join("\n");
|
|
217
|
-
const allJSDocs = sourceFile.getDescendantsOfKind(SyntaxKind.JSDoc);
|
|
218
|
-
const typeDefs = this.parseTypeDefs(allJSDocs);
|
|
219
|
-
/* Extract parameter descriptions from JSDoc */
|
|
220
|
-
const funcParam = funcDecl.getParameters()[0];
|
|
221
|
-
const params = {};
|
|
222
|
-
if (funcParam) {
|
|
223
|
-
/**
|
|
224
|
-
* Extract default values from the destructured parameter
|
|
225
|
-
* (ObjectBindingPattern ā BindingElement initializer)
|
|
226
|
-
*/
|
|
227
|
-
const defaults = new Map();
|
|
228
|
-
const nameNode = funcParam.getNameNode();
|
|
229
|
-
if (Node.isObjectBindingPattern(nameNode)) {
|
|
230
|
-
nameNode.getElements().forEach((el) => {
|
|
231
|
-
if (!Node.isBindingElement(el))
|
|
232
|
-
return;
|
|
233
|
-
const name = el.getName();
|
|
234
|
-
const initializer = el.getInitializer()?.getText();
|
|
235
|
-
if (initializer) {
|
|
236
|
-
defaults.set(name, initializer);
|
|
237
|
-
}
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
const paramProperties = funcParam.getType().getProperties();
|
|
241
|
-
const inputTypeDef = typeDefs["input"] ?? {};
|
|
242
|
-
if (paramProperties.length > 0 && !typeDefs["input"]) {
|
|
243
|
-
throw new Error(`[Action Validation] Action "${actionName}" in ${file} has parameters but is missing an ` +
|
|
244
|
-
`"@typedef {Object} input" JSDoc block. Please document all parameters with ` +
|
|
245
|
-
`@typedef {Object} input and @property tags.` +
|
|
246
|
-
`Run \`pulse skill fix ${actionName}\` to automatically add a JSDoc template for this action.`);
|
|
247
|
-
}
|
|
248
|
-
paramProperties.forEach((prop) => {
|
|
249
|
-
const name = prop.getName();
|
|
250
|
-
if (!inputTypeDef[name]) {
|
|
251
|
-
throw new Error(`[Action Validation] Action "${actionName}" in ${file}: parameter "${name}" is missing ` +
|
|
252
|
-
`a @property entry in the "input" JSDoc typedef. Please add ` +
|
|
253
|
-
`"@property {type} ${name} - description" to the JSDoc.` +
|
|
254
|
-
`Run \`pulse skill fix ${actionName}\` to automatically add a JSDoc template for this action.`);
|
|
255
|
-
}
|
|
256
|
-
if (!inputTypeDef[name]?.description?.trim()) {
|
|
257
|
-
throw new Error(`[Action Validation] Action "${actionName}" in ${file}: parameter "${name}" has an empty ` +
|
|
258
|
-
`description in the JSDoc @property. Please provide a meaningful description.` +
|
|
259
|
-
`Run \`pulse skill fix ${actionName}\` to automatically add a JSDoc template for this action.`);
|
|
260
|
-
}
|
|
261
|
-
const variable = {
|
|
262
|
-
description: inputTypeDef[name]?.description ?? "",
|
|
263
|
-
type: this.getType(inputTypeDef[name]?.type ?? ""),
|
|
264
|
-
optional: prop.isOptional() ? true : undefined,
|
|
265
|
-
defaultValue: defaults.get(name),
|
|
266
|
-
};
|
|
267
|
-
params[name] = variable;
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
/* Extract return type from JSDoc */
|
|
271
|
-
const rawReturnType = funcDecl.getReturnType();
|
|
272
|
-
const isPromiseLikeReturn = this.isPromiseLikeType(rawReturnType);
|
|
273
|
-
const returnType = this.unwrapPromiseLikeType(rawReturnType);
|
|
274
|
-
// Check if the return type is an object
|
|
275
|
-
if (!returnType.isObject()) {
|
|
276
|
-
console.warn(`[Action Registration] Function ${actionName}'s return type should be an object. Skipping...`);
|
|
277
|
-
return;
|
|
278
|
-
}
|
|
279
|
-
const returns = {};
|
|
280
|
-
const returnProperties = returnType.getProperties();
|
|
281
|
-
const outputTypeDef = typeDefs["output"] ?? {};
|
|
282
|
-
const hasOutputTypeDef = !!typeDefs["output"];
|
|
283
|
-
if (returnProperties.length > 0 &&
|
|
284
|
-
!hasOutputTypeDef &&
|
|
285
|
-
!isPromiseLikeReturn) {
|
|
286
|
-
throw new Error(`[Action Validation] Action "${actionName}" in ${file} returns properties but is missing an ` +
|
|
287
|
-
`"@typedef {Output}" JSDoc block. Please document all return values with ` +
|
|
288
|
-
`@typedef {Output} and @property tags.` +
|
|
289
|
-
`Run \`pulse skill fix ${actionName}\` to automatically add a JSDoc template for this action.`);
|
|
290
|
-
}
|
|
291
|
-
if (returnProperties.length > 0 && !hasOutputTypeDef && isPromiseLikeReturn) {
|
|
292
|
-
console.warn(`[Action Validation] Action "${actionName}" in ${file} is missing an "@typedef {Object} output" JSDoc block. ` +
|
|
293
|
-
`Falling back to TypeScript-inferred return metadata because the action returns a Promise.`);
|
|
294
|
-
}
|
|
295
|
-
returnProperties.forEach((prop) => {
|
|
296
|
-
const name = prop.getName();
|
|
297
|
-
if (!hasOutputTypeDef) {
|
|
298
|
-
const variable = {
|
|
299
|
-
description: "",
|
|
300
|
-
type: this.getType(prop.getTypeAtLocation(funcDecl).getText()),
|
|
301
|
-
optional: prop.isOptional() ? true : undefined,
|
|
302
|
-
defaultValue: undefined,
|
|
303
|
-
};
|
|
304
|
-
returns[name] = variable;
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
if (!outputTypeDef[name]) {
|
|
308
|
-
throw new Error(`[Action Validation] Action "${actionName}" in ${file}: return property "${name}" is missing ` +
|
|
309
|
-
`a @property entry in the "output" JSDoc typedef. Please add ` +
|
|
310
|
-
`"@property {type} ${name} - description" to the JSDoc.` +
|
|
311
|
-
`Run \`pulse skill fix ${actionName}\` to automatically add a JSDoc template for this action.`);
|
|
312
|
-
}
|
|
313
|
-
if (!outputTypeDef[name]?.description?.trim()) {
|
|
314
|
-
throw new Error(`[Action Validation] Action "${actionName}" in ${file}: return property "${name}" has an empty ` +
|
|
315
|
-
`description in the JSDoc @property. Please provide a meaningful description.` +
|
|
316
|
-
`Run \`pulse skill fix ${actionName}\` to automatically add a JSDoc template for this action.`);
|
|
317
|
-
}
|
|
318
|
-
const variable = {
|
|
319
|
-
description: outputTypeDef[name]?.description ?? "",
|
|
320
|
-
type: this.getType(outputTypeDef[name]?.type ?? ""),
|
|
321
|
-
optional: prop.isOptional() ? true : undefined,
|
|
322
|
-
defaultValue: undefined,
|
|
323
|
-
};
|
|
324
|
-
returns[name] = variable;
|
|
325
|
-
});
|
|
326
|
-
actions.push({
|
|
327
|
-
name: actionName,
|
|
328
|
-
description,
|
|
329
|
-
parameters: params,
|
|
330
|
-
returns,
|
|
331
|
-
});
|
|
332
|
-
});
|
|
333
|
-
});
|
|
334
|
-
// You can now register `actions` in Module Federation or expose them as needed
|
|
335
|
-
// Register actions in pulse config for runtime access
|
|
336
|
-
this.pulseConfig.actions = actions;
|
|
337
|
-
}
|
|
338
|
-
parseTypeDefs(jsDocs) {
|
|
339
|
-
const typeDefs = {};
|
|
340
|
-
// Match @typedef {Type} Name
|
|
341
|
-
const typedefRegex = /@typedef\s+{([^}]+)}\s+([^\s-]+)/g;
|
|
342
|
-
// Match @property {Type} [name] Description text...
|
|
343
|
-
const propertyRegex = /@property\s+{([^}]+)}\s+(\[?[^\]\s]+\]?)\s*-?\s*(.*)/g;
|
|
344
|
-
jsDocs.forEach((doc) => {
|
|
345
|
-
const text = doc.getFullText();
|
|
346
|
-
let typedefMatches;
|
|
347
|
-
while ((typedefMatches = typedefRegex.exec(text)) !== null) {
|
|
348
|
-
const typeName = typedefMatches[2];
|
|
349
|
-
if (!typeName)
|
|
350
|
-
continue;
|
|
351
|
-
const properties = {};
|
|
352
|
-
let propertyMatches;
|
|
353
|
-
while ((propertyMatches = propertyRegex.exec(text)) !== null) {
|
|
354
|
-
const propName = this.normalizeJSDocPropertyName(propertyMatches[2]);
|
|
355
|
-
const propType = propertyMatches[1];
|
|
356
|
-
const propDescription = propertyMatches[3] || "";
|
|
357
|
-
if (propName && propType) {
|
|
358
|
-
properties[propName] = {
|
|
359
|
-
type: propType,
|
|
360
|
-
description: propDescription.trim(),
|
|
361
|
-
};
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
typeDefs[typeName.toLowerCase()] = properties;
|
|
365
|
-
}
|
|
366
|
-
});
|
|
367
|
-
return typeDefs;
|
|
368
|
-
}
|
|
369
|
-
normalizeJSDocPropertyName(name) {
|
|
370
|
-
if (!name)
|
|
371
|
-
return "";
|
|
372
|
-
return name
|
|
373
|
-
.trim()
|
|
374
|
-
.replace(/^\[/, "")
|
|
375
|
-
.replace(/\]$/, "")
|
|
376
|
-
.split("=")[0]
|
|
377
|
-
?.trim();
|
|
378
|
-
}
|
|
379
|
-
isPromiseLikeType(type) {
|
|
380
|
-
const symbolName = type.getSymbol()?.getName();
|
|
381
|
-
return symbolName === "Promise" || symbolName === "PromiseLike";
|
|
382
|
-
}
|
|
383
|
-
unwrapPromiseLikeType(type) {
|
|
384
|
-
const symbolName = type.getSymbol()?.getName();
|
|
385
|
-
if ((symbolName === "Promise" || symbolName === "PromiseLike") &&
|
|
386
|
-
type.getTypeArguments().length > 0) {
|
|
387
|
-
return type.getTypeArguments()[0] ?? type;
|
|
388
|
-
}
|
|
389
|
-
return type;
|
|
390
|
-
}
|
|
391
|
-
getType(text) {
|
|
392
|
-
if (text === "string")
|
|
393
|
-
return "string";
|
|
394
|
-
if (text === "number")
|
|
395
|
-
return "number";
|
|
396
|
-
if (text === "boolean")
|
|
397
|
-
return "boolean";
|
|
398
|
-
if (text === "any")
|
|
399
|
-
return "object";
|
|
400
|
-
if (text.endsWith("[]"))
|
|
401
|
-
return [this.getType(text.slice(0, -2))];
|
|
402
|
-
if (text.length === 0)
|
|
403
|
-
return "undefined";
|
|
404
|
-
console.warn(`[Type Warning] Unrecognized type "${text}". Consider adding explicit types in your action's JSDoc comments for better type safety and documentation.`);
|
|
405
|
-
return text;
|
|
406
|
-
}
|
|
407
159
|
printChanges(compiler) {
|
|
408
160
|
const modified = compiler.modifiedFiles
|
|
409
161
|
? Array.from(compiler.modifiedFiles)
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { Configuration as WebpackConfig } from "webpack";
|
|
2
2
|
import { Configuration as DevServerConfig } from "webpack-dev-server";
|
|
3
|
+
export declare function makeMFPreviewConfig(): Promise<WebpackConfig>;
|
|
3
4
|
export declare function makePreviewClientConfig(mode: "development" | "production"): Promise<WebpackConfig & DevServerConfig>;
|
|
@@ -1,10 +1,133 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import mfNode from "@module-federation/node";
|
|
2
3
|
import CopyWebpackPlugin from "copy-webpack-plugin";
|
|
3
4
|
import fs from "fs";
|
|
4
5
|
import HtmlWebpackPlugin from "html-webpack-plugin";
|
|
5
6
|
import MiniCssExtractPlugin from "mini-css-extract-plugin";
|
|
6
7
|
import path from "path";
|
|
7
|
-
import
|
|
8
|
+
import wp from "webpack";
|
|
9
|
+
import { compileAppActionSkills, discoverAppSkillActions, getLocalNetworkIP, loadPulseConfig, } from "./utils.js";
|
|
10
|
+
const { NodeFederationPlugin } = mfNode;
|
|
11
|
+
const { webpack } = wp;
|
|
12
|
+
class MFPreviewPlugin {
|
|
13
|
+
projectDirName;
|
|
14
|
+
pulseConfig;
|
|
15
|
+
constructor(pulseConfig) {
|
|
16
|
+
this.projectDirName = process.cwd();
|
|
17
|
+
this.pulseConfig = pulseConfig;
|
|
18
|
+
}
|
|
19
|
+
apply(compiler) {
|
|
20
|
+
let isFirstRun = true;
|
|
21
|
+
compiler.hooks.environment.tap("WatchSkillPlugin", () => {
|
|
22
|
+
compiler.hooks.thisCompilation.tap("WatchActions", (compilation) => {
|
|
23
|
+
compilation.contextDependencies.add(path.resolve(this.projectDirName, "src/skill"));
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
compiler.hooks.watchRun.tap("MFPreviewPlugin", async () => {
|
|
27
|
+
if (!isFirstRun) {
|
|
28
|
+
const isActionChange = compiler.modifiedFiles
|
|
29
|
+
? Array.from(compiler.modifiedFiles).some((file) => file.includes("src/skill"))
|
|
30
|
+
: false;
|
|
31
|
+
if (isActionChange) {
|
|
32
|
+
console.log("[preview-server] Detected changes in actions. Recompiling...");
|
|
33
|
+
await this.compileActions(compiler);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
console.log("[preview-server] š Building...");
|
|
38
|
+
await this.compileActions(compiler);
|
|
39
|
+
console.log("[preview-server] ā
Successfully built preview server.");
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
compiler.hooks.done.tap("MFPreviewPlugin", () => {
|
|
43
|
+
if (isFirstRun) {
|
|
44
|
+
isFirstRun = false;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
console.log("[preview-server] ā
Reload finished.");
|
|
48
|
+
}
|
|
49
|
+
fs.writeFileSync(path.resolve(this.projectDirName, "dist/pulse.config.json"), JSON.stringify(this.pulseConfig, null, 2));
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
makeNodeFederationPlugin() {
|
|
53
|
+
const actions = discoverAppSkillActions();
|
|
54
|
+
return new NodeFederationPlugin({
|
|
55
|
+
name: this.pulseConfig.id + "_server",
|
|
56
|
+
remoteType: "script",
|
|
57
|
+
useRuntimePlugin: true,
|
|
58
|
+
library: { type: "commonjs-module" },
|
|
59
|
+
filename: "remoteEntry.js",
|
|
60
|
+
exposes: { ...actions },
|
|
61
|
+
}, {});
|
|
62
|
+
}
|
|
63
|
+
async compileActions(compiler) {
|
|
64
|
+
compileAppActionSkills(this.pulseConfig);
|
|
65
|
+
const options = {
|
|
66
|
+
...compiler.options,
|
|
67
|
+
watch: false,
|
|
68
|
+
plugins: [this.makeNodeFederationPlugin()],
|
|
69
|
+
};
|
|
70
|
+
const newCompiler = webpack(options);
|
|
71
|
+
return new Promise((resolve, reject) => {
|
|
72
|
+
newCompiler?.run((err, stats) => {
|
|
73
|
+
if (err) {
|
|
74
|
+
console.error("[preview-server] ā Error during recompilation:", err);
|
|
75
|
+
reject(err);
|
|
76
|
+
}
|
|
77
|
+
else if (stats?.hasErrors()) {
|
|
78
|
+
console.error("[preview-server] ā Compilation errors:", stats.toJson().errors);
|
|
79
|
+
reject(new Error("Compilation errors"));
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
console.log("[preview-server] ā
Compiled actions successfully.");
|
|
83
|
+
resolve();
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
export async function makeMFPreviewConfig() {
|
|
90
|
+
const projectDirName = process.cwd();
|
|
91
|
+
const pulseConfig = await loadPulseConfig();
|
|
92
|
+
return {
|
|
93
|
+
mode: "development",
|
|
94
|
+
name: "preview-server",
|
|
95
|
+
entry: {},
|
|
96
|
+
target: "async-node",
|
|
97
|
+
output: {
|
|
98
|
+
publicPath: "auto",
|
|
99
|
+
path: path.resolve(projectDirName, "dist/server"),
|
|
100
|
+
},
|
|
101
|
+
resolve: {
|
|
102
|
+
extensions: [".ts", ".js"],
|
|
103
|
+
},
|
|
104
|
+
plugins: [new MFPreviewPlugin(pulseConfig)],
|
|
105
|
+
module: {
|
|
106
|
+
rules: [
|
|
107
|
+
{
|
|
108
|
+
test: /\.tsx?$/,
|
|
109
|
+
use: {
|
|
110
|
+
loader: "ts-loader",
|
|
111
|
+
options: {
|
|
112
|
+
configFile: "node_modules/.pulse/tsconfig.server.json",
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
exclude: [/node_modules/, /dist/],
|
|
116
|
+
},
|
|
117
|
+
],
|
|
118
|
+
},
|
|
119
|
+
stats: {
|
|
120
|
+
all: false,
|
|
121
|
+
errors: true,
|
|
122
|
+
warnings: true,
|
|
123
|
+
logging: "warn",
|
|
124
|
+
colors: true,
|
|
125
|
+
},
|
|
126
|
+
infrastructureLogging: {
|
|
127
|
+
level: "warn",
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
8
131
|
class PreviewClientPlugin {
|
|
9
132
|
projectDirName;
|
|
10
133
|
pulseConfig;
|
|
@@ -50,8 +173,6 @@ class PreviewClientPlugin {
|
|
|
50
173
|
else {
|
|
51
174
|
console.log("[client-preview] ā
Reload finished");
|
|
52
175
|
}
|
|
53
|
-
// Write pulse config to dist
|
|
54
|
-
fs.writeFileSync(path.resolve(this.projectDirName, "dist/pulse.config.json"), JSON.stringify(this.pulseConfig, null, 2));
|
|
55
176
|
});
|
|
56
177
|
}
|
|
57
178
|
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { TypedVariableType } from "@pulse-editor/shared-utils/dist/types/types.js";
|
|
2
|
+
import { JSDoc } from "ts-morph";
|
|
1
3
|
export declare function loadPulseConfig(): Promise<any>;
|
|
2
4
|
export declare function getLocalNetworkIP(): string;
|
|
3
5
|
export declare function readConfigFile(): Promise<any>;
|
|
@@ -7,4 +9,13 @@ export declare function discoverServerFunctions(): {
|
|
|
7
9
|
export declare function discoverAppSkillActions(): {
|
|
8
10
|
[x: string]: string;
|
|
9
11
|
} | null;
|
|
12
|
+
export declare function parseTypeDefs(jsDocs: JSDoc[]): Record<string, Record<string, {
|
|
13
|
+
type: string;
|
|
14
|
+
description: string;
|
|
15
|
+
}>>;
|
|
16
|
+
export declare function normalizeJSDocPropertyName(name: string | undefined): string | undefined;
|
|
17
|
+
export declare function isPromiseLikeType(type: import("ts-morph").Type): boolean;
|
|
18
|
+
export declare function unwrapPromiseLikeType(type: import("ts-morph").Type): import("ts-morph").Type<import("ts-morph").ts.Type>;
|
|
19
|
+
export declare function getActionType(text: string): TypedVariableType;
|
|
20
|
+
export declare function compileAppActionSkills(pulseConfig: any): void;
|
|
10
21
|
export declare function generateTempTsConfig(): void;
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
1
|
import { existsSync, writeFileSync } from "fs";
|
|
3
2
|
import fs from "fs/promises";
|
|
4
3
|
import { globSync } from "glob";
|
|
5
4
|
import { networkInterfaces } from "os";
|
|
6
5
|
import path from "path";
|
|
7
|
-
import { Project, SyntaxKind } from "ts-morph";
|
|
6
|
+
import { Node, Project, SyntaxKind } from "ts-morph";
|
|
8
7
|
import ts from "typescript";
|
|
9
8
|
import { pathToFileURL } from "url";
|
|
10
9
|
export async function loadPulseConfig() {
|
|
@@ -143,6 +142,226 @@ export function discoverAppSkillActions() {
|
|
|
143
142
|
}, {});
|
|
144
143
|
return entryPoints;
|
|
145
144
|
}
|
|
145
|
+
export function parseTypeDefs(jsDocs) {
|
|
146
|
+
const typeDefs = {};
|
|
147
|
+
const typedefRegex = /@typedef\s+{([^}]+)}\s+([^\s-]+)/g;
|
|
148
|
+
const propertyRegex = /@property\s+{([^}]+)}\s+(\[?[^\]\s]+\]?)\s*-?\s*(.*)/g;
|
|
149
|
+
jsDocs.forEach((doc) => {
|
|
150
|
+
const text = doc.getFullText();
|
|
151
|
+
let typedefMatches;
|
|
152
|
+
while ((typedefMatches = typedefRegex.exec(text)) !== null) {
|
|
153
|
+
const typeName = typedefMatches[2];
|
|
154
|
+
if (!typeName)
|
|
155
|
+
continue;
|
|
156
|
+
const properties = {};
|
|
157
|
+
let propertyMatches;
|
|
158
|
+
while ((propertyMatches = propertyRegex.exec(text)) !== null) {
|
|
159
|
+
const propName = normalizeJSDocPropertyName(propertyMatches[2]);
|
|
160
|
+
const propType = propertyMatches[1];
|
|
161
|
+
const propDescription = propertyMatches[3] || "";
|
|
162
|
+
if (propName && propType) {
|
|
163
|
+
properties[propName] = {
|
|
164
|
+
type: propType,
|
|
165
|
+
description: propDescription.trim(),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
typeDefs[typeName.toLowerCase()] = properties;
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
return typeDefs;
|
|
173
|
+
}
|
|
174
|
+
export function normalizeJSDocPropertyName(name) {
|
|
175
|
+
if (!name)
|
|
176
|
+
return "";
|
|
177
|
+
return name
|
|
178
|
+
.trim()
|
|
179
|
+
.replace(/^\[/, "")
|
|
180
|
+
.replace(/\]$/, "")
|
|
181
|
+
.split("=")[0]
|
|
182
|
+
?.trim();
|
|
183
|
+
}
|
|
184
|
+
export function isPromiseLikeType(type) {
|
|
185
|
+
const symbolName = type.getSymbol()?.getName();
|
|
186
|
+
return symbolName === "Promise" || symbolName === "PromiseLike";
|
|
187
|
+
}
|
|
188
|
+
export function unwrapPromiseLikeType(type) {
|
|
189
|
+
const symbolName = type.getSymbol()?.getName();
|
|
190
|
+
if ((symbolName === "Promise" || symbolName === "PromiseLike") &&
|
|
191
|
+
type.getTypeArguments().length > 0) {
|
|
192
|
+
return type.getTypeArguments()[0] ?? type;
|
|
193
|
+
}
|
|
194
|
+
return type;
|
|
195
|
+
}
|
|
196
|
+
export function getActionType(text) {
|
|
197
|
+
if (text === "string")
|
|
198
|
+
return "string";
|
|
199
|
+
if (text === "number")
|
|
200
|
+
return "number";
|
|
201
|
+
if (text === "boolean")
|
|
202
|
+
return "boolean";
|
|
203
|
+
if (text === "any")
|
|
204
|
+
return "object";
|
|
205
|
+
if (text.endsWith("[]"))
|
|
206
|
+
return [getActionType(text.slice(0, -2))];
|
|
207
|
+
if (text.length === 0)
|
|
208
|
+
return "undefined";
|
|
209
|
+
console.warn(`[Type Warning] Unrecognized type "${text}". Consider adding explicit types in your action's JSDoc comments for better type safety and documentation.`);
|
|
210
|
+
return text;
|
|
211
|
+
}
|
|
212
|
+
export function compileAppActionSkills(pulseConfig) {
|
|
213
|
+
const files = globSync("./src/skill/*/action.ts");
|
|
214
|
+
const project = new Project({
|
|
215
|
+
tsConfigFilePath: path.join(process.cwd(), "node_modules/.pulse/tsconfig.server.json"),
|
|
216
|
+
});
|
|
217
|
+
const actions = [];
|
|
218
|
+
files.forEach((file) => {
|
|
219
|
+
const sourceFile = project.addSourceFileAtPath(file);
|
|
220
|
+
const defaultExportSymbol = sourceFile.getDefaultExportSymbol();
|
|
221
|
+
if (!defaultExportSymbol)
|
|
222
|
+
return;
|
|
223
|
+
const defaultExportDeclarations = defaultExportSymbol.getDeclarations();
|
|
224
|
+
defaultExportDeclarations.forEach((declaration) => {
|
|
225
|
+
if (declaration.getKind() !== SyntaxKind.FunctionDeclaration)
|
|
226
|
+
return;
|
|
227
|
+
const funcDecl = declaration.asKindOrThrow(SyntaxKind.FunctionDeclaration);
|
|
228
|
+
const pattern = /src\/skill\/([^\/]+)\/action\.ts$/;
|
|
229
|
+
const match = file.replaceAll("\\", "/").match(pattern);
|
|
230
|
+
if (!match) {
|
|
231
|
+
console.warn(`File path ${file} does not match pattern ${pattern}. Skipping...`);
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
const actionName = match[1];
|
|
235
|
+
if (!actionName) {
|
|
236
|
+
console.warn(`Could not extract action name from file path ${file}. Skipping...`);
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
if (actions.some((action) => action.name === actionName)) {
|
|
240
|
+
throw new Error(`Duplicate action name "${actionName}" detected in file ${file}. Please ensure all actions have unique names to avoid conflicts.`);
|
|
241
|
+
}
|
|
242
|
+
const defaultExportJSDocs = funcDecl.getJsDocs();
|
|
243
|
+
const descriptionText = defaultExportJSDocs
|
|
244
|
+
.map((doc) => doc.getDescription().replace(/^\*+/gm, "").trim())
|
|
245
|
+
.join("\n")
|
|
246
|
+
.trim();
|
|
247
|
+
if (defaultExportJSDocs.length === 0 || !descriptionText) {
|
|
248
|
+
throw new Error(`[Action Validation] Action "${actionName}" in ${file} is missing a JSDoc description. ` +
|
|
249
|
+
`Please add a JSDoc comment block with a description above the function.` +
|
|
250
|
+
`Run \`pulse skill fix ${actionName}\` to automatically add a JSDoc template for this action.`);
|
|
251
|
+
}
|
|
252
|
+
const description = defaultExportJSDocs
|
|
253
|
+
.map((doc) => doc.getFullText())
|
|
254
|
+
.join("\n");
|
|
255
|
+
const allJSDocs = sourceFile.getDescendantsOfKind(SyntaxKind.JSDoc);
|
|
256
|
+
const typeDefs = parseTypeDefs(allJSDocs);
|
|
257
|
+
const funcParam = funcDecl.getParameters()[0];
|
|
258
|
+
const params = {};
|
|
259
|
+
if (funcParam) {
|
|
260
|
+
const defaults = new Map();
|
|
261
|
+
const nameNode = funcParam.getNameNode();
|
|
262
|
+
if (Node.isObjectBindingPattern(nameNode)) {
|
|
263
|
+
nameNode.getElements().forEach((el) => {
|
|
264
|
+
if (!Node.isBindingElement(el))
|
|
265
|
+
return;
|
|
266
|
+
const name = el.getName();
|
|
267
|
+
const initializer = el.getInitializer()?.getText();
|
|
268
|
+
if (initializer) {
|
|
269
|
+
defaults.set(name, initializer);
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
const paramProperties = funcParam.getType().getProperties();
|
|
274
|
+
const inputTypeDef = typeDefs["input"] ?? {};
|
|
275
|
+
if (paramProperties.length > 0 && !typeDefs["input"]) {
|
|
276
|
+
throw new Error(`[Action Validation] Action "${actionName}" in ${file} has parameters but is missing an ` +
|
|
277
|
+
`"@typedef {Object} input" JSDoc block. Please document all parameters with ` +
|
|
278
|
+
`@typedef {Object} input and @property tags.` +
|
|
279
|
+
`Run \`pulse skill fix ${actionName}\` to automatically add a JSDoc template for this action.`);
|
|
280
|
+
}
|
|
281
|
+
paramProperties.forEach((prop) => {
|
|
282
|
+
const name = prop.getName();
|
|
283
|
+
if (!inputTypeDef[name]) {
|
|
284
|
+
throw new Error(`[Action Validation] Action "${actionName}" in ${file}: parameter "${name}" is missing ` +
|
|
285
|
+
`a @property entry in the "input" JSDoc typedef. Please add ` +
|
|
286
|
+
`"@property {type} ${name} - description" to the JSDoc.` +
|
|
287
|
+
`Run \`pulse skill fix ${actionName}\` to automatically add a JSDoc template for this action.`);
|
|
288
|
+
}
|
|
289
|
+
if (!inputTypeDef[name]?.description?.trim()) {
|
|
290
|
+
throw new Error(`[Action Validation] Action "${actionName}" in ${file}: parameter "${name}" has an empty ` +
|
|
291
|
+
`description in the JSDoc @property. Please provide a meaningful description.` +
|
|
292
|
+
`Run \`pulse skill fix ${actionName}\` to automatically add a JSDoc template for this action.`);
|
|
293
|
+
}
|
|
294
|
+
const variable = {
|
|
295
|
+
description: inputTypeDef[name]?.description ?? "",
|
|
296
|
+
type: getActionType(inputTypeDef[name]?.type ?? ""),
|
|
297
|
+
optional: prop.isOptional() ? true : undefined,
|
|
298
|
+
defaultValue: defaults.get(name),
|
|
299
|
+
};
|
|
300
|
+
params[name] = variable;
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
const rawReturnType = funcDecl.getReturnType();
|
|
304
|
+
const isPromiseLikeReturn = isPromiseLikeType(rawReturnType);
|
|
305
|
+
const returnType = unwrapPromiseLikeType(rawReturnType);
|
|
306
|
+
if (!returnType.isObject()) {
|
|
307
|
+
console.warn(`[Action Registration] Function ${actionName}'s return type should be an object. Skipping...`);
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
const returns = {};
|
|
311
|
+
const returnProperties = returnType.getProperties();
|
|
312
|
+
const outputTypeDef = typeDefs["output"] ?? {};
|
|
313
|
+
const hasOutputTypeDef = !!typeDefs["output"];
|
|
314
|
+
if (returnProperties.length > 0 && !hasOutputTypeDef && !isPromiseLikeReturn) {
|
|
315
|
+
throw new Error(`[Action Validation] Action "${actionName}" in ${file} returns properties but is missing an ` +
|
|
316
|
+
`"@typedef {Output}" JSDoc block. Please document all return values with ` +
|
|
317
|
+
`@typedef {Output} and @property tags.` +
|
|
318
|
+
`Run \`pulse skill fix ${actionName}\` to automatically add a JSDoc template for this action.`);
|
|
319
|
+
}
|
|
320
|
+
if (returnProperties.length > 0 && !hasOutputTypeDef && isPromiseLikeReturn) {
|
|
321
|
+
console.warn(`[Action Validation] Action "${actionName}" in ${file} is missing an "@typedef {Object} output" JSDoc block. ` +
|
|
322
|
+
`Falling back to TypeScript-inferred return metadata because the action returns a Promise.`);
|
|
323
|
+
}
|
|
324
|
+
returnProperties.forEach((prop) => {
|
|
325
|
+
const name = prop.getName();
|
|
326
|
+
if (!hasOutputTypeDef) {
|
|
327
|
+
const variable = {
|
|
328
|
+
description: "",
|
|
329
|
+
type: getActionType(prop.getTypeAtLocation(funcDecl).getText()),
|
|
330
|
+
optional: prop.isOptional() ? true : undefined,
|
|
331
|
+
defaultValue: undefined,
|
|
332
|
+
};
|
|
333
|
+
returns[name] = variable;
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
if (!outputTypeDef[name]) {
|
|
337
|
+
throw new Error(`[Action Validation] Action "${actionName}" in ${file}: return property "${name}" is missing ` +
|
|
338
|
+
`a @property entry in the "output" JSDoc typedef. Please add ` +
|
|
339
|
+
`"@property {type} ${name} - description" to the JSDoc.` +
|
|
340
|
+
`Run \`pulse skill fix ${actionName}\` to automatically add a JSDoc template for this action.`);
|
|
341
|
+
}
|
|
342
|
+
if (!outputTypeDef[name]?.description?.trim()) {
|
|
343
|
+
throw new Error(`[Action Validation] Action "${actionName}" in ${file}: return property "${name}" has an empty ` +
|
|
344
|
+
`description in the JSDoc @property. Please provide a meaningful description.` +
|
|
345
|
+
`Run \`pulse skill fix ${actionName}\` to automatically add a JSDoc template for this action.`);
|
|
346
|
+
}
|
|
347
|
+
const variable = {
|
|
348
|
+
description: outputTypeDef[name]?.description ?? "",
|
|
349
|
+
type: getActionType(outputTypeDef[name]?.type ?? ""),
|
|
350
|
+
optional: prop.isOptional() ? true : undefined,
|
|
351
|
+
defaultValue: undefined,
|
|
352
|
+
};
|
|
353
|
+
returns[name] = variable;
|
|
354
|
+
});
|
|
355
|
+
actions.push({
|
|
356
|
+
name: actionName,
|
|
357
|
+
description,
|
|
358
|
+
parameters: params,
|
|
359
|
+
returns,
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
pulseConfig.actions = actions;
|
|
364
|
+
}
|
|
146
365
|
// Generate tsconfig for server functions
|
|
147
366
|
export function generateTempTsConfig() {
|
|
148
367
|
const tempTsConfigPath = path.join(process.cwd(), "node_modules/.pulse/tsconfig.server.json");
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
import { makeMFClientConfig } from "./configs/mf-client.js";
|
|
3
3
|
import { makeMFServerConfig } from "./configs/mf-server.js";
|
|
4
|
-
import { makePreviewClientConfig } from "./configs/preview.js";
|
|
4
|
+
import { makeMFPreviewConfig, makePreviewClientConfig } from "./configs/preview.js";
|
|
5
5
|
export async function createWebpackConfig(isPreview, buildTarget, mode) {
|
|
6
6
|
if (isPreview) {
|
|
7
7
|
const previewClientConfig = await makePreviewClientConfig("development");
|
|
8
|
-
const
|
|
9
|
-
return [previewClientConfig,
|
|
8
|
+
const mfPreviewConfig = await makeMFPreviewConfig();
|
|
9
|
+
return [previewClientConfig, mfPreviewConfig];
|
|
10
10
|
}
|
|
11
11
|
else if (buildTarget === "server") {
|
|
12
12
|
const mfServerConfig = await makeMFServerConfig(mode);
|