69 lines
2.9 KiB
TypeScript
69 lines
2.9 KiB
TypeScript
/**
|
||
* rpa-core CLI host(任务 5.1;迁移阶段1-T4 重写):
|
||
* Node/Bun 直接拉起 createEngine(),按需显式注册 5 个能力域(不再副作用全量注册),
|
||
* 能力调用统一走 engine.execute(ADR-R1 语义真实化),验证包的零宿主可运行性。
|
||
*
|
||
* 跑通 5 个代表能力(全部只读或落在临时目录,可重复执行):
|
||
* 1. 桌面(PowerShell user32):Window_GetActiveWindow —— 读前台窗口标题
|
||
* 2. Excel(exceljs): Excel_Cs_NewV5 —— 临时目录新建工作簿
|
||
* 3. HTTP(fetch): Http_Get —— GET 百度首页
|
||
* 4. 文件(fs): File_Write / File_Read
|
||
* 5. 窗口/屏幕(PowerShell): Window_GetScreenSize
|
||
*
|
||
* 运行:npm run rpa-cli
|
||
*/
|
||
import fs from "node:fs";
|
||
import os from "node:os";
|
||
import path from "node:path";
|
||
import {
|
||
createEngine,
|
||
capabilityCount,
|
||
registerAll,
|
||
type RunContext
|
||
} from "@rpa/core";
|
||
|
||
const ctx = {
|
||
runId: "rpa-cli",
|
||
flowId: "rpa-cli",
|
||
flow: { id: "rpa-cli", name: "rpa-cli", nodes: [], edges: [] },
|
||
vars: {},
|
||
secrets: {},
|
||
signal: new AbortController().signal,
|
||
evidenceDir: "",
|
||
log: (message: string) => console.log(" " + message),
|
||
componentOf: () => undefined
|
||
} as unknown as RunContext;
|
||
const node = (componentId: string) => ({ id: "rpa-cli", componentId, position: { x: 0, y: 0 }, values: {}, options: {} });
|
||
|
||
async function run(engine: import("@rpa/core").RpaEngine, name: string, args: Record<string, unknown>): Promise<void> {
|
||
process.stdout.write(`[${name}] `);
|
||
const result = await engine.execute(name, args, ctx, node(name));
|
||
console.log("OK ->", typeof result === "string" ? result.slice(0, 120) : JSON.stringify(result)?.slice(0, 200));
|
||
}
|
||
|
||
async function main(): Promise<void> {
|
||
const engine = createEngine();
|
||
// 显式注册本 CLI 用到的 5 个能力域(迁移后 Agent-Employee 侧同样按域注册)
|
||
await registerAll(engine, ["desktop", "commonFinal", "excelCommon", "files", "http"]);
|
||
console.log(`rpa-core CLI host:引擎已创建(注入 driver 可用 createEngine({ driver })),注册能力 ${capabilityCount()} 个\n`);
|
||
|
||
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "rpa-cli-"));
|
||
try {
|
||
await run(engine, "Window_GetActiveWindow", {});
|
||
await run(engine, "Window_GetScreenSize", {});
|
||
await run(engine, "Excel_Cs_NewV5", { filePath: tmpDir, name: "rpa-cli-demo.xlsx" });
|
||
const file = path.join(tmpDir, "hello.txt");
|
||
await run(engine, "File_Write", { path: file, content: "hello from rpa-core CLI" });
|
||
await run(engine, "File_Read", { path: file });
|
||
await run(engine, "Http_Get", { url: "https://www.baidu.com", timeout: 15 });
|
||
console.log("\n5 项代表能力全部通过 ✓");
|
||
} finally {
|
||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||
}
|
||
}
|
||
|
||
void main().catch((error) => {
|
||
console.error("\nCLI host 失败:", (error as Error).message);
|
||
process.exit(1);
|
||
});
|