-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.ts
More file actions
97 lines (87 loc) · 3.56 KB
/
Copy pathplugin.ts
File metadata and controls
97 lines (87 loc) · 3.56 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
import type { Plugin } from "@opencode-ai/plugin";
import { readFile } from "node:fs/promises";
import path from "node:path";
import { AgentLoader } from "./src/agent-loader";
import { AssetPermissionRegistrar } from "./src/permission-registrar";
import { Installer } from "./src/installer";
const PACKAGE_NAME = "opencode-architect";
export class OpencodeArchitectPlugin {
private readonly agentsDir: string;
private readonly assetsDir: string;
private readonly readVersion: () => Promise<string>;
private readonly hasInstall: (projectDir: string) => Promise<boolean>;
private failureAdvised = false;
private notInstalledAdvised = false;
constructor(
agentsDir: string | null = null,
assetsDir: string | null = null,
readVersion: (() => Promise<string>) | null = null,
hasInstall: ((projectDir: string) => Promise<boolean>) | null = null,
) {
this.agentsDir = agentsDir ?? path.join(import.meta.dirname, "agents");
this.assetsDir = assetsDir ?? import.meta.dirname;
this.readVersion = readVersion ?? (() => this.readPackageMetadata());
this.hasInstall = hasInstall ?? ((projectDir) => new Installer().hasManifestAnywhere(projectDir));
}
public toPlugin(): Plugin {
return async (input) => {
const directory = typeof input?.directory === "string" ? input.directory : process.cwd();
return {
config: async (config) => {
try {
const agents = await new AgentLoader(this.agentsDir).loadAgents();
config.agent = config.agent || {};
for (const [name, agentConfig] of Object.entries(agents)) {
config.agent[name] = agentConfig;
}
new AssetPermissionRegistrar(this.assetsDir).register(config);
await this.adviseNotInstalledOnce(directory);
} catch (error) {
await this.adviseFailureOnce(error instanceof Error ? error.message : String(error));
}
},
};
};
}
private async adviseNotInstalledOnce(directory: string): Promise<void> {
if (this.notInstalledAdvised) return;
if (await this.hasInstall(directory)) return;
this.notInstalledAdvised = true;
try {
console.log(
`[${PACKAGE_NAME}] Not installed in any scope. Run: bunx ${PACKAGE_NAME} install --scope global ` +
`(or: bunx ${PACKAGE_NAME} install).`,
);
} catch {}
}
private async adviseFailureOnce(message: string): Promise<void> {
if (this.failureAdvised) return;
this.failureAdvised = true;
await this.emitAdvisory(message);
}
private async emitAdvisory(message: string): Promise<void> {
const prefix =
`Failed to load the bundled agent suite. Run: bunx ${PACKAGE_NAME} clear-cache, then ` +
`reinstall and restart OpenCode. The stale cache copy is `;
let text: string;
try {
text = `${prefix}~/.cache/opencode/packages/${PACKAGE_NAME}@${await this.readVersion()}. Cause: ${message}`;
} catch {
text = `${prefix}~/.cache/opencode/packages/${PACKAGE_NAME}@<version>. Cause: ${message}`;
}
try {
console.warn(`[${PACKAGE_NAME}] ${text}`);
} catch {}
}
private async readPackageMetadata(): Promise<string> {
const manifest = JSON.parse(
await readFile(path.join(import.meta.dirname, "package.json"), "utf-8"),
) as { version: string };
if (typeof manifest.version !== "string" || manifest.version.length === 0) {
throw new Error("unreadable package metadata");
}
return manifest.version;
}
}
const opencodeArchitect = new OpencodeArchitectPlugin();
export default opencodeArchitect.toPlugin();