Repository navigation
Expand file tree
/
Copy pathopencode-plugin.js
More file actions
144 lines (117 loc) · 3.71 KB
/
Copy pathopencode-plugin.js
File metadata and controls
144 lines (117 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { readdir, readFile } from "node:fs/promises";
import { basename, dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { Plugin } from "@opencode/plugin";
import { parse as parseYaml } from "yaml";
const pluginRoot = dirname(fileURLToPath(import.meta.url));
const profiles = Object.freeze({
base: ["base"],
nextjs: ["base", "nextjs"],
turborepo: ["base", "turborepo"],
"nextjs-turbo": ["base", "nextjs", "turborepo"],
});
function selectedProfile(options) {
const configuredProfile =
typeof options?.profile === "string" ? options.profile.trim() : "";
const requestedProfile =
configuredProfile || process.env.OPENPOWERS_PROFILE?.trim() || "base";
if (Object.hasOwn(profiles, requestedProfile)) {
return requestedProfile;
}
console.warn(
`[openpowers] Unknown profile "${requestedProfile}". Falling back to "base".`,
);
return "base";
}
function skillDirectoriesFor(profile) {
return profiles[profile].map((group) => join(pluginRoot, "skills", group));
}
async function findSkillFiles(directory) {
const entries = await readdir(directory, { withFileTypes: true });
const files = [];
for (const entry of entries.sort((left, right) =>
left.name.localeCompare(right.name),
)) {
const path = join(directory, entry.name);
if (entry.isDirectory()) {
files.push(...(await findSkillFiles(path)));
} else if (entry.isFile() && entry.name === "SKILL.md") {
files.push(path);
}
}
return files;
}
function splitFrontmatter(source, path) {
if (!source.startsWith("---")) {
return { attributes: {}, content: source };
}
const match = source.match(/^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/);
if (!match) {
throw new Error(`[openpowers] Invalid YAML frontmatter in ${path}`);
}
const attributes = parseYaml(match[1]) ?? {};
if (typeof attributes !== "object" || Array.isArray(attributes)) {
throw new Error(`[openpowers] Frontmatter must be a map in ${path}`);
}
return {
attributes,
content: source.slice(match[0].length),
};
}
function modelInvocationSetting(attributes) {
const metadata = attributes.metadata;
if (
metadata &&
typeof metadata === "object" &&
!Array.isArray(metadata) &&
typeof metadata["opencode/autoinvoke"] === "boolean"
) {
return metadata["opencode/autoinvoke"];
}
if (typeof attributes["disable-model-invocation"] === "boolean") {
return !attributes["disable-model-invocation"];
}
return undefined;
}
async function loadSkill(path) {
const source = await readFile(path, "utf8");
const { attributes, content } = splitFrontmatter(source, path);
const id = basename(dirname(path));
const name = typeof attributes.name === "string" ? attributes.name : id;
const description =
typeof attributes.description === "string"
? attributes.description
: undefined;
const autoinvoke = modelInvocationSetting(attributes);
return {
id,
name,
...(description === undefined ? {} : { description }),
...(autoinvoke === undefined ? {} : { autoinvoke }),
path,
content,
};
}
async function loadProfileSkills(profile) {
const skills = new Map();
for (const directory of skillDirectoriesFor(profile)) {
for (const path of await findSkillFiles(directory)) {
const skill = await loadSkill(path);
skills.set(skill.id, skill);
}
}
return [...skills.values()];
}
const OpenPowersPlugin = Plugin.define({
id: "openpowers",
async setup(ctx) {
const skills = await loadProfileSkills(selectedProfile(ctx.options));
await ctx.skill.transform((editor) => {
for (const skill of skills) {
editor.add(skill);
}
});
},
});
export { OpenPowersPlugin };
export default OpenPowersPlugin;