@eworkbase/plugin-ai-rules 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/plugin.mjs +271 -6
- package/dist/ui/main.js +119 -20
- package/dist/ui/style.css +839 -0
- package/package.json +5 -3
package/dist/plugin.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import { existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSyn
|
|
|
2
2
|
import os from "node:os";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { isDeepStrictEqual } from "node:util";
|
|
5
|
+
import { createHash } from "node:crypto";
|
|
5
6
|
//#region ../../node_modules/.pnpm/zod@4.6.5/node_modules/zod/v4/core/util.js
|
|
6
7
|
function getEnumValues(entries) {
|
|
7
8
|
const numericValues = Object.values(entries).filter((v) => typeof v === "number");
|
|
@@ -8350,6 +8351,263 @@ async function waitForOperation(pending, signal) {
|
|
|
8350
8351
|
}
|
|
8351
8352
|
}
|
|
8352
8353
|
//#endregion
|
|
8354
|
+
//#region src/Resources.dto.ts
|
|
8355
|
+
/** Project-owned AI resources and revision-checked editing contracts. */
|
|
8356
|
+
let ResourceKind = /* @__PURE__ */ function(ResourceKind) {
|
|
8357
|
+
ResourceKind["Rules"] = "rules";
|
|
8358
|
+
ResourceKind["Skills"] = "skills";
|
|
8359
|
+
ResourceKind["Mcp"] = "mcp";
|
|
8360
|
+
ResourceKind["Commands"] = "commands";
|
|
8361
|
+
ResourceKind["Subagents"] = "subagents";
|
|
8362
|
+
return ResourceKind;
|
|
8363
|
+
}({});
|
|
8364
|
+
const RESOURCE_LIMIT = 1048576;
|
|
8365
|
+
const resourceIdentitySchema = object({
|
|
8366
|
+
kind: _enum(ResourceKind),
|
|
8367
|
+
id: string().regex(/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/)
|
|
8368
|
+
}).strict();
|
|
8369
|
+
const resourceWriteSchema = resourceIdentitySchema.extend({
|
|
8370
|
+
revision: string().nullable(),
|
|
8371
|
+
files: array(object({
|
|
8372
|
+
path: string().min(1).max(240),
|
|
8373
|
+
content: string().max(RESOURCE_LIMIT)
|
|
8374
|
+
}).strict()).min(1).max(128)
|
|
8375
|
+
}).strict();
|
|
8376
|
+
const resourceDeleteSchema = resourceIdentitySchema.extend({ revision: string() }).strict();
|
|
8377
|
+
const resourceImportSchema = object({ directory: string().min(1).max(4096) }).strict();
|
|
8378
|
+
//#endregion
|
|
8379
|
+
//#region src/resources.ts
|
|
8380
|
+
/** Reads and edits canonical Rulesync sources; client generation remains in the plugin. */
|
|
8381
|
+
function registerResources(context, hooks) {
|
|
8382
|
+
const root = context.project.root;
|
|
8383
|
+
const catalog = () => ({ resources: resourceDocuments(root).map(({ files, ...summary }) => ({
|
|
8384
|
+
...summary,
|
|
8385
|
+
paths: files.map(({ path }) => path)
|
|
8386
|
+
})) });
|
|
8387
|
+
hooks.registerStatus({
|
|
8388
|
+
id: "resources",
|
|
8389
|
+
label: "AI 资源",
|
|
8390
|
+
description: "只读列出项目规则、技能和 MCP。",
|
|
8391
|
+
resolve: catalog
|
|
8392
|
+
});
|
|
8393
|
+
hooks.registerStatus({
|
|
8394
|
+
id: "resource",
|
|
8395
|
+
label: "资源内容",
|
|
8396
|
+
description: "只读查看完整资源及其文件。",
|
|
8397
|
+
input: resourceIdentitySchema,
|
|
8398
|
+
resolve: (input) => requireResource(root, resourceIdentitySchema.parse(input))
|
|
8399
|
+
});
|
|
8400
|
+
hooks.registerStatus({
|
|
8401
|
+
id: "skill.references",
|
|
8402
|
+
label: "Skill 关联",
|
|
8403
|
+
description: "检查引用此 Skill 的工作流。",
|
|
8404
|
+
input: resourceIdentitySchema,
|
|
8405
|
+
resolve: (input, invocation) => references(resourceIdentitySchema.parse(input), invocation)
|
|
8406
|
+
});
|
|
8407
|
+
hooks.registerStatus({
|
|
8408
|
+
id: "skill.import",
|
|
8409
|
+
label: "导入 Skill 预览",
|
|
8410
|
+
description: "只读检查所选目录,不保存文件。",
|
|
8411
|
+
input: resourceImportSchema,
|
|
8412
|
+
resolve: (input) => importSkill(context, resourceImportSchema.parse(input).directory)
|
|
8413
|
+
});
|
|
8414
|
+
hooks.registerCommand({
|
|
8415
|
+
id: "resource.save",
|
|
8416
|
+
title: "保存 AI 资源",
|
|
8417
|
+
input: resourceWriteSchema,
|
|
8418
|
+
transaction: PluginCommandTransaction.ProjectMutation,
|
|
8419
|
+
plan: (input) => ({
|
|
8420
|
+
kind: PluginSharedChangeKind.Files,
|
|
8421
|
+
paths: [...writePlan(root, resourceWriteSchema.parse(input)).keys()].map(sharedPath)
|
|
8422
|
+
}),
|
|
8423
|
+
run: async (input, invocation) => {
|
|
8424
|
+
const request = resourceWriteSchema.parse(input);
|
|
8425
|
+
const desired = writePlan(root, request);
|
|
8426
|
+
invocation.signal.throwIfAborted();
|
|
8427
|
+
writeFileBatch(root, desired);
|
|
8428
|
+
return requireResource(root, request);
|
|
8429
|
+
}
|
|
8430
|
+
});
|
|
8431
|
+
hooks.registerCommand({
|
|
8432
|
+
id: "resource.delete",
|
|
8433
|
+
title: "删除 AI 资源",
|
|
8434
|
+
input: resourceDeleteSchema,
|
|
8435
|
+
transaction: PluginCommandTransaction.ProjectMutation,
|
|
8436
|
+
plan: (input) => ({
|
|
8437
|
+
kind: PluginSharedChangeKind.Files,
|
|
8438
|
+
paths: [...deletePlan(root, resourceDeleteSchema.parse(input)).keys()].map(sharedPath)
|
|
8439
|
+
}),
|
|
8440
|
+
run: async (input, invocation) => {
|
|
8441
|
+
const request = resourceDeleteSchema.parse(input);
|
|
8442
|
+
const usages = await references(request, invocation);
|
|
8443
|
+
if (usages.length) throw new Error(`无法删除,以下工作流仍在引用:${usages.map(({ title }) => title).join("、")}。请先修改关联工作流。`);
|
|
8444
|
+
const desired = deletePlan(root, request);
|
|
8445
|
+
invocation.signal.throwIfAborted();
|
|
8446
|
+
writeFileBatch(root, desired);
|
|
8447
|
+
return catalog();
|
|
8448
|
+
}
|
|
8449
|
+
});
|
|
8450
|
+
}
|
|
8451
|
+
function resourceDocuments(root) {
|
|
8452
|
+
const sources = safeFilePath(root, "rules/.rulesync");
|
|
8453
|
+
if (!existsSync(sources)) return [];
|
|
8454
|
+
const documents = [];
|
|
8455
|
+
for (const kind of [
|
|
8456
|
+
"rules",
|
|
8457
|
+
"skills",
|
|
8458
|
+
"commands",
|
|
8459
|
+
"subagents"
|
|
8460
|
+
]) {
|
|
8461
|
+
const directory = safeFilePath(root, `rules/.rulesync/${kind}`);
|
|
8462
|
+
if (!existsSync(directory)) continue;
|
|
8463
|
+
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
|
8464
|
+
const id = kind === "skills" ? entry.name : entry.name.replace(/\.md$/, "");
|
|
8465
|
+
if (!resourceIdentitySchema.safeParse({
|
|
8466
|
+
kind,
|
|
8467
|
+
id
|
|
8468
|
+
}).success) continue;
|
|
8469
|
+
if (kind !== "skills" && !entry.name.endsWith(".md")) continue;
|
|
8470
|
+
const source = `rules/.rulesync/${kind}/${entry.name}`;
|
|
8471
|
+
const target = safeFilePath(root, source);
|
|
8472
|
+
const contents = kind === "skills" && entry.isDirectory() ? new Map([...readOutputFiles(target)].map(([file, content]) => [`${source}/${file}`, content])) : /* @__PURE__ */ new Map([[source, readSafeFile(root, source)]]);
|
|
8473
|
+
if (kind === "skills" && !contents.has(`${source}/SKILL.md`)) continue;
|
|
8474
|
+
documents.push(document({
|
|
8475
|
+
kind,
|
|
8476
|
+
id
|
|
8477
|
+
}, contents));
|
|
8478
|
+
}
|
|
8479
|
+
}
|
|
8480
|
+
const { content } = mcpSource(root);
|
|
8481
|
+
const servers = mcpServers(content);
|
|
8482
|
+
Object.entries(servers).forEach(([id, server]) => {
|
|
8483
|
+
resourceIdentitySchema.parse({
|
|
8484
|
+
kind: "mcp",
|
|
8485
|
+
id
|
|
8486
|
+
});
|
|
8487
|
+
documents.push(document({
|
|
8488
|
+
kind: "mcp",
|
|
8489
|
+
id
|
|
8490
|
+
}, /* @__PURE__ */ new Map([["server.json", JSON.stringify(server, null, 2)]])));
|
|
8491
|
+
});
|
|
8492
|
+
return documents.sort((left, right) => `${left.kind}/${left.id}`.localeCompare(`${right.kind}/${right.id}`));
|
|
8493
|
+
}
|
|
8494
|
+
function document(identity, contents) {
|
|
8495
|
+
const files = [...contents].sort(([left], [right]) => left.localeCompare(right)).map(([path, content]) => ({
|
|
8496
|
+
path,
|
|
8497
|
+
content
|
|
8498
|
+
}));
|
|
8499
|
+
const serialized = JSON.stringify(files);
|
|
8500
|
+
if (Buffer.byteLength(serialized) > 1048576 || files.some(({ content }) => content.includes("\0"))) throw new Error("资源超过 1 MiB 或包含二进制内容,请使用文本资源。");
|
|
8501
|
+
return {
|
|
8502
|
+
...identity,
|
|
8503
|
+
files,
|
|
8504
|
+
paths: files.map(({ path }) => path),
|
|
8505
|
+
revision: createHash("sha256").update(serialized).digest("hex")
|
|
8506
|
+
};
|
|
8507
|
+
}
|
|
8508
|
+
function requireResource(root, identity) {
|
|
8509
|
+
const resource = resourceDocuments(root).find(({ kind, id }) => kind === identity.kind && id === identity.id);
|
|
8510
|
+
if (!resource) throw new Error("资源已不存在,请刷新列表。");
|
|
8511
|
+
return resource;
|
|
8512
|
+
}
|
|
8513
|
+
function checkRevision(root, request) {
|
|
8514
|
+
const current = resourceDocuments(root).find(({ kind, id }) => kind === request.kind && id === request.id);
|
|
8515
|
+
if ((current?.revision ?? null) !== request.revision) throw new Error("资源已变化或同名资源已存在,请重新查看后编辑。");
|
|
8516
|
+
return current;
|
|
8517
|
+
}
|
|
8518
|
+
function writePlan(root, request) {
|
|
8519
|
+
const previous = checkRevision(root, request);
|
|
8520
|
+
if (request.kind === "mcp") {
|
|
8521
|
+
if (request.files.length !== 1) throw new Error("MCP 配置只能包含一个服务对象。");
|
|
8522
|
+
const server = parseObject(request.files[0].content);
|
|
8523
|
+
if (!(typeof server.command === "string" && server.command.trim()) && !(typeof server.url === "string" && /^https?:\/\//.test(server.url))) throw new Error("MCP 必须提供启动命令或 HTTP 地址。");
|
|
8524
|
+
return updateMcp(root, request.id, server);
|
|
8525
|
+
}
|
|
8526
|
+
const prefix = `rules/.rulesync/${request.kind}/${request.id}`;
|
|
8527
|
+
const expected = request.kind === "skills" ? `${prefix}/SKILL.md` : `${prefix}.md`;
|
|
8528
|
+
const files = new Map(request.files.map(({ path: source, content }) => {
|
|
8529
|
+
safeFilePath(root, source);
|
|
8530
|
+
if (request.kind === "skills" ? !source.startsWith(`${prefix}/`) : source !== expected) throw new Error("资源文件不能超出当前条目范围。");
|
|
8531
|
+
return [source, content];
|
|
8532
|
+
}));
|
|
8533
|
+
if (files.size !== request.files.length || !files.has(expected)) throw new Error("资源文件重复或缺少正文文件。");
|
|
8534
|
+
document(request, files);
|
|
8535
|
+
for (const source of files.keys()) if (!previous?.paths.includes(source) && readSafeFile(root, source) !== null) throw new Error("目标文件已存在,请先查看已有内容。");
|
|
8536
|
+
return new Map([...(previous?.paths ?? []).map((source) => [source, null]), ...files]);
|
|
8537
|
+
}
|
|
8538
|
+
function deletePlan(root, request) {
|
|
8539
|
+
const resource = checkRevision(root, request);
|
|
8540
|
+
if (!resource) throw new Error("资源已不存在,请刷新列表。");
|
|
8541
|
+
return request.kind === "mcp" ? updateMcp(root, request.id, void 0) : new Map(resource.paths.map((source) => [source, null]));
|
|
8542
|
+
}
|
|
8543
|
+
function sharedPath(source) {
|
|
8544
|
+
return `../${source}`;
|
|
8545
|
+
}
|
|
8546
|
+
function mcpSource(root) {
|
|
8547
|
+
const source = readSafeFile(root, "rules/.rulesync/mcp.jsonc") !== null ? "rules/.rulesync/mcp.jsonc" : "rules/.rulesync/mcp.json";
|
|
8548
|
+
return {
|
|
8549
|
+
path: source,
|
|
8550
|
+
content: readSafeFile(root, source) ?? "{\n \"mcpServers\": {}\n}\n"
|
|
8551
|
+
};
|
|
8552
|
+
}
|
|
8553
|
+
function parseObject(content) {
|
|
8554
|
+
const errors = [];
|
|
8555
|
+
const parsed = parse$1(content, errors);
|
|
8556
|
+
if (errors.length || !parsed || typeof parsed !== "object" || Array.isArray(parsed)) throw new Error("MCP JSON 必须是有效对象,请检查格式。");
|
|
8557
|
+
return record(string(), json()).parse(parsed);
|
|
8558
|
+
}
|
|
8559
|
+
function mcpServers(content) {
|
|
8560
|
+
return record(string(), json()).parse(parseObject(content).mcpServers ?? {});
|
|
8561
|
+
}
|
|
8562
|
+
function updateMcp(root, id, server) {
|
|
8563
|
+
const source = mcpSource(root);
|
|
8564
|
+
mcpServers(source.content);
|
|
8565
|
+
const edited = applyEdits(source.content, modify(source.content, ["mcpServers", id], server, { formattingOptions: {
|
|
8566
|
+
insertSpaces: true,
|
|
8567
|
+
tabSize: 2
|
|
8568
|
+
} }));
|
|
8569
|
+
return /* @__PURE__ */ new Map([[source.path, edited]]);
|
|
8570
|
+
}
|
|
8571
|
+
async function references(identity, invocation) {
|
|
8572
|
+
if (identity.kind !== "skills") return [];
|
|
8573
|
+
if (!invocation.services.capabilities.catalog().statuses.some(({ pluginId, id }) => pluginId === "ai-workflows" && id === "catalog")) throw new Error("无法检查工作流关联,请先准备 AI Workflows 后重试。");
|
|
8574
|
+
const catalog = object({
|
|
8575
|
+
workflows: array(object({
|
|
8576
|
+
id: string(),
|
|
8577
|
+
title: string(),
|
|
8578
|
+
skill: string()
|
|
8579
|
+
})),
|
|
8580
|
+
error: string().nullable()
|
|
8581
|
+
}).parse(await invocation.services.capabilities.resolveStatus({
|
|
8582
|
+
pluginId: "ai-workflows",
|
|
8583
|
+
id: "catalog",
|
|
8584
|
+
input: {}
|
|
8585
|
+
}));
|
|
8586
|
+
if (catalog.error) throw new Error("工作流配置无效,无法确认资源关联,请先修复工作流配置。");
|
|
8587
|
+
return catalog.workflows.filter(({ skill }) => skill === identity.id).map(({ id, title }) => ({
|
|
8588
|
+
id,
|
|
8589
|
+
title
|
|
8590
|
+
}));
|
|
8591
|
+
}
|
|
8592
|
+
function importSkill(context, directory) {
|
|
8593
|
+
const root = [context.project.root, context.workspace.root].find((candidate) => {
|
|
8594
|
+
const relative = path.relative(candidate, directory);
|
|
8595
|
+
return relative && !relative.startsWith("..") && !path.isAbsolute(relative);
|
|
8596
|
+
});
|
|
8597
|
+
if (!root) throw new Error("请选择项目或当前工作区内的 Skill 目录。");
|
|
8598
|
+
const relative = path.relative(root, directory).split(path.sep).join("/");
|
|
8599
|
+
const source = safeFilePath(root, relative);
|
|
8600
|
+
if (!lstatSync(source).isDirectory() || realpathSync(source) !== path.resolve(realpathSync(root), relative)) throw new Error("Skill 目录无效或包含链接。");
|
|
8601
|
+
const id = path.basename(source);
|
|
8602
|
+
const identity = resourceIdentitySchema.parse({
|
|
8603
|
+
kind: "skills",
|
|
8604
|
+
id
|
|
8605
|
+
});
|
|
8606
|
+
const files = new Map([...readOutputFiles(source)].map(([file, content]) => [`rules/.rulesync/skills/${id}/${file}`, content]));
|
|
8607
|
+
if (!files.has(`rules/.rulesync/skills/${id}/SKILL.md`)) throw new Error("所选目录缺少 SKILL.md。");
|
|
8608
|
+
return document(identity, files);
|
|
8609
|
+
}
|
|
8610
|
+
//#endregion
|
|
8353
8611
|
//#region src/plugin.ts
|
|
8354
8612
|
/**
|
|
8355
8613
|
* Uses Rulesync as the replaceable engine for project AI rules and client files.
|
|
@@ -8379,6 +8637,7 @@ var plugin_default = definePlugin({
|
|
|
8379
8637
|
id: "ai-rules",
|
|
8380
8638
|
title: "AI Rules",
|
|
8381
8639
|
setup(context, hooks) {
|
|
8640
|
+
registerResources(context, hooks);
|
|
8382
8641
|
const rulesRoot = path.join(context.project.root, RULES_DIRECTORY);
|
|
8383
8642
|
let config = resolveConfig(context.config);
|
|
8384
8643
|
const paths = resolvePaths({
|
|
@@ -8478,12 +8737,18 @@ var plugin_default = definePlugin({
|
|
|
8478
8737
|
}), invocation), invocation);
|
|
8479
8738
|
const initialize = async (invocation) => {
|
|
8480
8739
|
mkdirSync(paths.sourceRoot, { recursive: true });
|
|
8481
|
-
|
|
8482
|
-
|
|
8483
|
-
|
|
8484
|
-
|
|
8485
|
-
|
|
8486
|
-
|
|
8740
|
+
mkdirSync(safeFilePath(context.project.root, "rules/.rulesync"), { recursive: true });
|
|
8741
|
+
if (readSafeFile(context.project.root, "rules/rulesync.jsonc") === null) writeFileBatch(context.project.root, /* @__PURE__ */ new Map([["rules/rulesync.jsonc", "{}\n"]]));
|
|
8742
|
+
const initializationResult = {
|
|
8743
|
+
command: RULESYNC_PROGRAM,
|
|
8744
|
+
completedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
8745
|
+
details: null,
|
|
8746
|
+
operation: "initialize",
|
|
8747
|
+
standardError: "",
|
|
8748
|
+
standardOutput: "已创建空规则配置。",
|
|
8749
|
+
success: true,
|
|
8750
|
+
warnings: []
|
|
8751
|
+
};
|
|
8487
8752
|
const result = initializationResult.success ? await runSynchronization({
|
|
8488
8753
|
initializationResult,
|
|
8489
8754
|
invocation,
|