my-opencode 1.2.1 → 1.3.1
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/README.md +86 -2
- package/bin/cli.js +37 -7
- package/bin/moc +199 -0
- package/bin/moc.js +350 -0
- package/opencode.json +15 -12
- package/package.json +4 -2
- package/plugins/README.md +64 -0
- package/plugins/provider-profile.mjs +139 -0
- package/scripts/postinstall.js +22 -2
package/README.md
CHANGED
|
@@ -9,17 +9,49 @@
|
|
|
9
9
|
## 快速安裝(npm)
|
|
10
10
|
|
|
11
11
|
```bash
|
|
12
|
-
# 在專案中安裝(產生 .opencode
|
|
12
|
+
# 在專案中安裝(產生 .opencode/,並提供 moc launcher)
|
|
13
|
+
npm install my-opencode
|
|
14
|
+
|
|
15
|
+
# 或只初始化 .opencode/
|
|
13
16
|
npx my-opencode init
|
|
14
17
|
|
|
15
18
|
# 安裝到指定專案
|
|
16
19
|
npx my-opencode init ./my-project
|
|
17
20
|
```
|
|
18
21
|
|
|
19
|
-
這會在你的專案下建立 `.opencode/`,包含 `skills/`、`agents/`、`opencode.json`。
|
|
22
|
+
這會在你的專案下建立 `.opencode/`,包含 `skills/`、`agents/`、`plugins/`、`opencode.json`。
|
|
20
23
|
|
|
21
24
|
opencode 會自動發現 `.opencode/opencode.json`,無需額外設定。
|
|
22
25
|
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
### 本地開發(git clone)
|
|
29
|
+
|
|
30
|
+
如果直接 clone 本 repo 做開發或試用:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
git clone https://github.com/jjyung/my-opencode.git
|
|
34
|
+
cd my-opencode
|
|
35
|
+
npm link # 將 bin/moc 註冊到 PATH,讓 moc 指令全域可用
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`npm link` 完成後,`moc` 即可在任何目錄使用。
|
|
39
|
+
新的 npm bin 入口是跨平台的 `bin/moc.js`;repo 內仍保留 `bin/moc` 給直接 shell 執行情境使用。
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
安裝完成後,`moc` 會成為主要入口:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
moc # 預設 opencode profile
|
|
47
|
+
moc openai # 切到 OpenAI profile
|
|
48
|
+
moc google # 切到 Google profile
|
|
49
|
+
moc copilot # 切到 Copilot profile
|
|
50
|
+
moc --help # 透明轉發到 opencode --help
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Windows(cmd、PowerShell)現在也支援直接執行 `moc`;npm 會自動建立對應 shim,無需額外安裝 `.cmd` 或 `.ps1` 檔案。
|
|
54
|
+
|
|
23
55
|
其他指令:
|
|
24
56
|
|
|
25
57
|
```bash
|
|
@@ -27,6 +59,58 @@ npx my-opencode list # 列出可用 skills 與 agents
|
|
|
27
59
|
npx my-opencode help # 顯示說明
|
|
28
60
|
```
|
|
29
61
|
|
|
62
|
+
## Provider Profile 切換
|
|
63
|
+
|
|
64
|
+
內建 `provider-profile` plugin / resolver,主要透過 `moc` 切換整套模型配置。
|
|
65
|
+
|
|
66
|
+
支援值:`opencode`(預設)、`openai`、`google`、`copilot`
|
|
67
|
+
|
|
68
|
+
| Profile | Primary / heavy | Small / verifier |
|
|
69
|
+
|---------|------------------|------------------|
|
|
70
|
+
| `opencode` | `opencode/big-pickle` | `opencode/deepseek-v4-flash-free` |
|
|
71
|
+
| `openai` | `openai/gpt-5.4` | `openai/gpt-5.4-mini` |
|
|
72
|
+
| `google` | `google/gemini-3.1-pro-preview-customtools` | `google/gemini-3.5-flash` |
|
|
73
|
+
| `copilot` | `copilot/gpt-5.4` | `copilot/gpt-5.4-mini` |
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
# 預設 profile(opencode)
|
|
77
|
+
moc
|
|
78
|
+
|
|
79
|
+
# 切到 OpenAI
|
|
80
|
+
moc openai
|
|
81
|
+
|
|
82
|
+
# 切到 Google
|
|
83
|
+
moc google
|
|
84
|
+
|
|
85
|
+
# 轉發額外參數
|
|
86
|
+
moc openai --list-agents
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`moc` 會在目前工作目錄尋找 `.opencode/plugins/provider-profile.mjs` 與 `.opencode/opencode.json`。
|
|
90
|
+
對 `opencode` / `openai` / `google` / `copilot` 這四個 profile,會把產生出的 `OPENCODE_CONFIG_CONTENT` 快取到 `.opencode/cache/provider-profile/`;只要 profile、plugin mtime、config mtime 都沒變,就直接重用快取,不再重新跑 Node。若 plugin 缺失,則會警告並回退為 plain `opencode`。
|
|
91
|
+
|
|
92
|
+
若你直接用 repo 內的 `bin/moc`,仍會走既有 POSIX shell launcher;透過 npm 安裝、`npm link`、`npx` 取得的 `moc` 則會走跨平台的 `bin/moc.js`。
|
|
93
|
+
|
|
94
|
+
若要在 script 中直接使用環境變數,也可以:
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
OPENCODE_PROVIDER_PROFILE=openai \
|
|
98
|
+
OPENCODE_CONFIG_CONTENT="$(node .opencode/plugins/provider-profile.mjs)" \
|
|
99
|
+
opencode
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
如果想固定專案 profile,可把 `.opencode/opencode.json` 的 plugin 設定改成 tuple:
|
|
103
|
+
|
|
104
|
+
```json
|
|
105
|
+
{
|
|
106
|
+
"plugin": [
|
|
107
|
+
["plugins/provider-profile.mjs", { "profile": "google" }]
|
|
108
|
+
]
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
此 tuple 會覆寫 `OPENCODE_PROVIDER_PROFILE`。
|
|
113
|
+
|
|
30
114
|
### 發布(維護者專用)
|
|
31
115
|
|
|
32
116
|
#### Token 設定
|
package/bin/cli.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
1
|
+
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* my-opencode CLI
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* → Files copied directly to .opencode/, no dependency in package.json
|
|
13
13
|
*
|
|
14
14
|
* Commands:
|
|
15
|
-
* init [target-dir] Install skills/agents into .opencode/
|
|
15
|
+
* init [target-dir] Install skills/agents/plugins into .opencode/
|
|
16
16
|
* list List available skills and agents
|
|
17
17
|
* help Show this help
|
|
18
18
|
*/
|
|
@@ -26,6 +26,7 @@ const PKG_ROOT = path.resolve(path.dirname(__filename), "..");
|
|
|
26
26
|
|
|
27
27
|
const SKILLS_DIR = path.join(PKG_ROOT, "skills");
|
|
28
28
|
const AGENTS_DIR = path.join(PKG_ROOT, "agents");
|
|
29
|
+
const PLUGINS_DIR = path.join(PKG_ROOT, "plugins");
|
|
29
30
|
const OPENCODE_CONFIG = path.join(PKG_ROOT, "opencode.json");
|
|
30
31
|
|
|
31
32
|
const INDENT = " ";
|
|
@@ -108,7 +109,7 @@ function cmdHelp() {
|
|
|
108
109
|
|
|
109
110
|
\x1b[36m方法 A: npm install\x1b[0m (自動複製到 .opencode/)
|
|
110
111
|
npm install my-opencode
|
|
111
|
-
✓ postinstall 自動將 agents/ skills/ 複製到 .opencode/
|
|
112
|
+
✓ postinstall 自動將 agents/ skills/ plugins/ 複製到 .opencode/
|
|
112
113
|
✓ 原始檔案留在 node_modules/ (已 gitignore)
|
|
113
114
|
|
|
114
115
|
\x1b[36m方法 B: npx\x1b[0m (不留 package.json dependency)
|
|
@@ -116,17 +117,24 @@ function cmdHelp() {
|
|
|
116
117
|
✓ 直接安裝到 .opencode/
|
|
117
118
|
✓ 不寫入 package.json
|
|
118
119
|
|
|
119
|
-
\x1b[1mCOMMANDS\x1b[0m
|
|
120
|
-
init [target-dir] 安裝 skills/agents 到 .opencode/ 下
|
|
120
|
+
\x1b[1mCOMMANDS\x1b[0m
|
|
121
|
+
init [target-dir] 安裝 skills/agents/plugins 到 .opencode/ 下
|
|
121
122
|
(預設: 當前目錄的 .opencode/)
|
|
122
123
|
list 列出可用的 skills 與 agents
|
|
123
124
|
help 顯示此說明
|
|
124
125
|
|
|
125
|
-
\x1b[
|
|
126
|
+
\x1b[1mLAUNCHER\x1b[0m
|
|
127
|
+
moc 預設使用 opencode profile 啟動 opencode
|
|
128
|
+
moc openai 使用 openai profile 啟動 opencode
|
|
129
|
+
moc google 使用 google profile 啟動 opencode
|
|
130
|
+
moc --help 透明轉發到 opencode --help
|
|
131
|
+
|
|
132
|
+
\x1b[1mEXAMPLES\x1b[0m
|
|
126
133
|
npm install my-opencode # 方法 A (自動)
|
|
127
134
|
npx my-opencode init # 方法 B (手動)
|
|
128
135
|
npx my-opencode init ./my-project # 指定專案目錄
|
|
129
136
|
npx my-opencode list # 列出內容
|
|
137
|
+
moc openai # 用 openai profile 啟動
|
|
130
138
|
`);
|
|
131
139
|
}
|
|
132
140
|
|
|
@@ -208,6 +216,25 @@ function cmdInit(targetDir) {
|
|
|
208
216
|
}
|
|
209
217
|
success(`Agents: ${agentCount} installed → .opencode/agents/`);
|
|
210
218
|
|
|
219
|
+
// ── Copy plugins ─────────────────────────────────────────────────────────
|
|
220
|
+
const targetPlugins = path.join(target, "plugins");
|
|
221
|
+
if (!fs.existsSync(targetPlugins)) {
|
|
222
|
+
fs.mkdirSync(targetPlugins, { recursive: true });
|
|
223
|
+
}
|
|
224
|
+
const plugins = getFiles(PLUGINS_DIR).filter((name) => name.endsWith(".mjs"));
|
|
225
|
+
let pluginCount = 0;
|
|
226
|
+
for (const plugin of plugins) {
|
|
227
|
+
const src = path.join(PLUGINS_DIR, plugin);
|
|
228
|
+
const dst = path.join(targetPlugins, plugin);
|
|
229
|
+
if (!fs.existsSync(dst)) {
|
|
230
|
+
fs.copyFileSync(src, dst);
|
|
231
|
+
pluginCount++;
|
|
232
|
+
} else {
|
|
233
|
+
warn(`Skipped (already exists): plugins/${plugin}`);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
success(`Plugins: ${pluginCount} installed → .opencode/plugins/`);
|
|
237
|
+
|
|
211
238
|
// ── Generate opencode.json ──────────────────────────────────────────────
|
|
212
239
|
const targetConfig = path.join(target, "opencode.json");
|
|
213
240
|
if (!fs.existsSync(targetConfig)) {
|
|
@@ -226,6 +253,7 @@ function cmdInit(targetDir) {
|
|
|
226
253
|
console.log();
|
|
227
254
|
header("Installation Complete");
|
|
228
255
|
info("my-opencode is ready at .opencode/");
|
|
256
|
+
info('Use "moc" as the primary launcher once the bin is on your PATH');
|
|
229
257
|
console.log();
|
|
230
258
|
console.log(`${INDENT}Your project's opencode.json should reference:`);
|
|
231
259
|
console.log(`${INDENT} "instructions": [`);
|
|
@@ -237,7 +265,7 @@ function cmdInit(targetDir) {
|
|
|
237
265
|
console.log(`${INDENT} ".opencode/skills/orchestrate/SKILL.md"`);
|
|
238
266
|
console.log(`${INDENT} ]`);
|
|
239
267
|
console.log();
|
|
240
|
-
console.log(`${INDENT}Agents and
|
|
268
|
+
console.log(`${INDENT}Agents, commands, and plugins from .opencode/opencode.json`);
|
|
241
269
|
console.log(`${INDENT}are auto-discovered by opencode.`);
|
|
242
270
|
console.log();
|
|
243
271
|
info("Documentation: https://github.com/jjyung/my-opencode");
|
|
@@ -303,6 +331,8 @@ function main() {
|
|
|
303
331
|
]\x1b[0m
|
|
304
332
|
|
|
305
333
|
更多資訊請執行:\x1b[36mnpx my-opencode help\x1b[0m
|
|
334
|
+
|
|
335
|
+
若已透過 npm install 安裝,也可直接使用:\x1b[36mmoc\x1b[0m / \x1b[36mmoc openai\x1b[0m
|
|
306
336
|
`);
|
|
307
337
|
break;
|
|
308
338
|
}
|
package/bin/moc
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
|
|
3
|
+
profile=
|
|
4
|
+
known_profiles="opencode openai google copilot"
|
|
5
|
+
|
|
6
|
+
print_help() {
|
|
7
|
+
cat <<'EOF'
|
|
8
|
+
Usage:
|
|
9
|
+
moc [profile] [opencode-args...]
|
|
10
|
+
moc --help
|
|
11
|
+
moc doctor [profile]
|
|
12
|
+
moc print-config [profile]
|
|
13
|
+
|
|
14
|
+
Profiles:
|
|
15
|
+
opencode | openai | google | copilot
|
|
16
|
+
|
|
17
|
+
Examples:
|
|
18
|
+
moc
|
|
19
|
+
moc google
|
|
20
|
+
moc openai --list-agents
|
|
21
|
+
moc doctor google
|
|
22
|
+
moc print-config google
|
|
23
|
+
|
|
24
|
+
Notes:
|
|
25
|
+
- `moc` without extra args launches interactive opencode.
|
|
26
|
+
- Use `moc doctor` or `moc print-config` for non-interactive debugging.
|
|
27
|
+
EOF
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
print_doctor() {
|
|
31
|
+
active_profile=$1
|
|
32
|
+
printf 'profile=%s\n' "$active_profile"
|
|
33
|
+
printf 'plugin_path=%s\n' "$plugin_path"
|
|
34
|
+
printf 'config_path=%s\n' "$config_path"
|
|
35
|
+
printf 'cache_content_path=%s\n' "$cache_content_path"
|
|
36
|
+
printf 'cache_meta_path=%s\n' "$cache_meta_path"
|
|
37
|
+
printf 'plugin_exists=%s\n' "$( [ -f "$plugin_path" ] && printf yes || printf no )"
|
|
38
|
+
printf 'config_exists=%s\n' "$( [ -f "$config_path" ] && printf yes || printf no )"
|
|
39
|
+
printf 'cache_content_exists=%s\n' "$( [ -f "$cache_content_path" ] && printf yes || printf no )"
|
|
40
|
+
printf 'cache_meta_exists=%s\n' "$( [ -f "$cache_meta_path" ] && printf yes || printf no )"
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
case "${1-}" in
|
|
44
|
+
--help|-h|help)
|
|
45
|
+
print_help
|
|
46
|
+
exit 0
|
|
47
|
+
;;
|
|
48
|
+
"")
|
|
49
|
+
profile=opencode
|
|
50
|
+
;;
|
|
51
|
+
doctor)
|
|
52
|
+
profile=${2-opencode}
|
|
53
|
+
;;
|
|
54
|
+
print-config)
|
|
55
|
+
profile=${2-opencode}
|
|
56
|
+
;;
|
|
57
|
+
opencode|openai|google|copilot)
|
|
58
|
+
profile=$1
|
|
59
|
+
shift
|
|
60
|
+
;;
|
|
61
|
+
esac
|
|
62
|
+
|
|
63
|
+
if [ -z "$profile" ]; then
|
|
64
|
+
exec opencode "$@"
|
|
65
|
+
fi
|
|
66
|
+
|
|
67
|
+
case "$profile" in
|
|
68
|
+
opencode|openai|google|copilot)
|
|
69
|
+
;;
|
|
70
|
+
*)
|
|
71
|
+
printf '%s\n' "moc: unknown profile '$profile'. Must be one of: $known_profiles" >&2
|
|
72
|
+
exit 1
|
|
73
|
+
;;
|
|
74
|
+
esac
|
|
75
|
+
|
|
76
|
+
exec_opencode_with_profile() {
|
|
77
|
+
exec env \
|
|
78
|
+
OPENCODE_PROVIDER_PROFILE="$profile" \
|
|
79
|
+
OPENCODE_CONFIG_CONTENT="$config_content" \
|
|
80
|
+
opencode "$@"
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
plugin_path=.opencode/plugins/provider-profile.mjs
|
|
84
|
+
config_path=.opencode/opencode.json
|
|
85
|
+
cache_dir=.opencode/cache/provider-profile
|
|
86
|
+
cache_content_path=$cache_dir/$profile.json
|
|
87
|
+
cache_meta_path=$cache_dir/$profile.meta
|
|
88
|
+
|
|
89
|
+
get_mtime() {
|
|
90
|
+
if stat -f %m "$1" >/dev/null 2>&1; then
|
|
91
|
+
stat -f %m "$1"
|
|
92
|
+
else
|
|
93
|
+
stat -c %Y "$1"
|
|
94
|
+
fi
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
read_meta() {
|
|
98
|
+
meta_profile=
|
|
99
|
+
meta_plugin_mtime=
|
|
100
|
+
meta_config_mtime=
|
|
101
|
+
|
|
102
|
+
[ -f "$1" ] || return 1
|
|
103
|
+
|
|
104
|
+
while IFS='=' read -r key value; do
|
|
105
|
+
case "$key" in
|
|
106
|
+
profile) meta_profile=$value ;;
|
|
107
|
+
plugin_mtime) meta_plugin_mtime=$value ;;
|
|
108
|
+
config_mtime) meta_config_mtime=$value ;;
|
|
109
|
+
esac
|
|
110
|
+
done < "$1"
|
|
111
|
+
|
|
112
|
+
[ -n "$meta_profile" ] && [ -n "$meta_plugin_mtime" ] && [ -n "$meta_config_mtime" ]
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
write_cache() {
|
|
116
|
+
tmp_content=$cache_content_path.tmp.$$
|
|
117
|
+
tmp_meta=$cache_meta_path.tmp.$$
|
|
118
|
+
|
|
119
|
+
if ! mkdir -p "$cache_dir" 2>/dev/null; then
|
|
120
|
+
return 1
|
|
121
|
+
fi
|
|
122
|
+
|
|
123
|
+
if ! printf '%s\n' "$1" > "$tmp_content"; then
|
|
124
|
+
rm -f "$tmp_content" "$tmp_meta"
|
|
125
|
+
return 1
|
|
126
|
+
fi
|
|
127
|
+
|
|
128
|
+
if ! {
|
|
129
|
+
printf 'profile=%s\n' "$profile"
|
|
130
|
+
printf 'plugin_mtime=%s\n' "$plugin_mtime"
|
|
131
|
+
printf 'config_mtime=%s\n' "$config_mtime"
|
|
132
|
+
} > "$tmp_meta"; then
|
|
133
|
+
rm -f "$tmp_content" "$tmp_meta"
|
|
134
|
+
return 1
|
|
135
|
+
fi
|
|
136
|
+
|
|
137
|
+
if ! mv "$tmp_content" "$cache_content_path" || ! mv "$tmp_meta" "$cache_meta_path"; then
|
|
138
|
+
rm -f "$tmp_content" "$tmp_meta"
|
|
139
|
+
return 1
|
|
140
|
+
fi
|
|
141
|
+
|
|
142
|
+
return 0
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if [ ! -f "$plugin_path" ]; then
|
|
146
|
+
printf '%s\n' "moc: plugin not found at $plugin_path; falling back to plain opencode" >&2
|
|
147
|
+
exec opencode "$@"
|
|
148
|
+
fi
|
|
149
|
+
|
|
150
|
+
if [ ! -f "$config_path" ]; then
|
|
151
|
+
printf '%s\n' "moc: config not found at $config_path; cannot compute provider profile override" >&2
|
|
152
|
+
exit 1
|
|
153
|
+
fi
|
|
154
|
+
|
|
155
|
+
case "${1-}" in
|
|
156
|
+
doctor)
|
|
157
|
+
print_doctor "$profile"
|
|
158
|
+
exit 0
|
|
159
|
+
;;
|
|
160
|
+
esac
|
|
161
|
+
|
|
162
|
+
plugin_mtime=$(get_mtime "$plugin_path") || {
|
|
163
|
+
printf '%s\n' "moc: failed to read plugin mtime from $plugin_path" >&2
|
|
164
|
+
exit 1
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
config_mtime=$(get_mtime "$config_path") || {
|
|
168
|
+
printf '%s\n' "moc: failed to read config mtime from $config_path" >&2
|
|
169
|
+
exit 1
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
config_content=
|
|
173
|
+
|
|
174
|
+
if read_meta "$cache_meta_path" && [ -f "$cache_content_path" ]; then
|
|
175
|
+
if [ "$meta_profile" = "$profile" ] \
|
|
176
|
+
&& [ "$meta_plugin_mtime" = "$plugin_mtime" ] \
|
|
177
|
+
&& [ "$meta_config_mtime" = "$config_mtime" ]; then
|
|
178
|
+
config_content=$(tr -d '\n' < "$cache_content_path") || config_content=
|
|
179
|
+
fi
|
|
180
|
+
fi
|
|
181
|
+
|
|
182
|
+
if [ -z "$config_content" ]; then
|
|
183
|
+
if ! config_content=$(OPENCODE_PROVIDER_PROFILE=$profile node "$plugin_path"); then
|
|
184
|
+
printf '%s\n' "moc: failed to compute OPENCODE_CONFIG_CONTENT from $plugin_path" >&2
|
|
185
|
+
exit 1
|
|
186
|
+
fi
|
|
187
|
+
|
|
188
|
+
write_cache "$config_content" || \
|
|
189
|
+
printf '%s\n' "moc: warning: failed to update cache at $cache_dir; continuing with fresh config" >&2
|
|
190
|
+
fi
|
|
191
|
+
|
|
192
|
+
case "${1-}" in
|
|
193
|
+
print-config)
|
|
194
|
+
printf '%s\n' "$config_content"
|
|
195
|
+
exit 0
|
|
196
|
+
;;
|
|
197
|
+
esac
|
|
198
|
+
|
|
199
|
+
exec_opencode_with_profile "$@"
|
package/bin/moc.js
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { constants as osConstants } from 'node:os';
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
7
|
+
|
|
8
|
+
const knownProfiles = new Set(['opencode', 'openai', 'google', 'copilot']);
|
|
9
|
+
const knownProfilesText = 'opencode openai google copilot';
|
|
10
|
+
const signalExitCodes = {
|
|
11
|
+
SIGHUP: 1,
|
|
12
|
+
SIGINT: 2,
|
|
13
|
+
SIGTERM: 15,
|
|
14
|
+
SIGBREAK: 21,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const pluginPath = path.join('.opencode', 'plugins', 'provider-profile.mjs');
|
|
18
|
+
const configPath = path.join('.opencode', 'opencode.json');
|
|
19
|
+
|
|
20
|
+
const pluginAbsPath = path.resolve(process.cwd(), pluginPath);
|
|
21
|
+
const configAbsPath = path.resolve(process.cwd(), configPath);
|
|
22
|
+
|
|
23
|
+
function isKnownProfile(value) {
|
|
24
|
+
return knownProfiles.has(value);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function normalizeProfile(value) {
|
|
28
|
+
return value.trim();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function getMtime(filePath, label) {
|
|
32
|
+
try {
|
|
33
|
+
return String(fs.statSync(filePath).mtimeMs);
|
|
34
|
+
} catch {
|
|
35
|
+
console.error(`moc: failed to read ${label} mtime from ${label === 'plugin' ? pluginPath : configPath}`);
|
|
36
|
+
process.exit(1);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function readMeta(filePath) {
|
|
41
|
+
try {
|
|
42
|
+
const raw = fs.readFileSync(filePath, 'utf8');
|
|
43
|
+
const meta = {};
|
|
44
|
+
|
|
45
|
+
for (const line of raw.split(/\r?\n/)) {
|
|
46
|
+
const index = line.indexOf('=');
|
|
47
|
+
if (index === -1) {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const key = line.slice(0, index);
|
|
52
|
+
const value = line.slice(index + 1);
|
|
53
|
+
meta[key] = value;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (meta.profile && meta.plugin_mtime && meta.config_mtime) {
|
|
57
|
+
return meta;
|
|
58
|
+
}
|
|
59
|
+
} catch {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function readCacheContent(filePath) {
|
|
67
|
+
try {
|
|
68
|
+
return fs.readFileSync(filePath, 'utf8').replace(/\r?\n/g, '');
|
|
69
|
+
} catch {
|
|
70
|
+
return '';
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function writeCache(cacheDir, cacheContentPath, cacheMetaPath, profile, pluginMtime, configMtime, configContent) {
|
|
75
|
+
const suffix = `.tmp.${process.pid}.${Date.now()}`;
|
|
76
|
+
const tempContentPath = `${cacheContentPath}${suffix}`;
|
|
77
|
+
const tempMetaPath = `${cacheMetaPath}${suffix}`;
|
|
78
|
+
const metaContent = [
|
|
79
|
+
`profile=${profile}`,
|
|
80
|
+
`plugin_mtime=${pluginMtime}`,
|
|
81
|
+
`config_mtime=${configMtime}`,
|
|
82
|
+
'',
|
|
83
|
+
].join('\n');
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
fs.mkdirSync(cacheDir, { recursive: true });
|
|
87
|
+
fs.writeFileSync(tempContentPath, `${configContent}\n`, 'utf8');
|
|
88
|
+
fs.writeFileSync(tempMetaPath, metaContent, 'utf8');
|
|
89
|
+
fs.renameSync(tempContentPath, cacheContentPath);
|
|
90
|
+
fs.renameSync(tempMetaPath, cacheMetaPath);
|
|
91
|
+
return true;
|
|
92
|
+
} catch {
|
|
93
|
+
try {
|
|
94
|
+
fs.rmSync(tempContentPath, { force: true });
|
|
95
|
+
} catch {
|
|
96
|
+
// ignore cleanup errors
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
fs.rmSync(tempMetaPath, { force: true });
|
|
101
|
+
} catch {
|
|
102
|
+
// ignore cleanup errors
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function computeConfigContent(profile) {
|
|
110
|
+
const result = spawnSync(process.execPath, [pluginAbsPath], {
|
|
111
|
+
env: {
|
|
112
|
+
...process.env,
|
|
113
|
+
OPENCODE_PROVIDER_PROFILE: profile,
|
|
114
|
+
},
|
|
115
|
+
encoding: 'utf8',
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
if (result.stderr) {
|
|
119
|
+
process.stderr.write(result.stderr);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (result.error || result.status !== 0) {
|
|
123
|
+
console.error(`moc: failed to compute OPENCODE_CONFIG_CONTENT from ${pluginPath}`);
|
|
124
|
+
process.exit(typeof result.status === 'number' && result.status !== 0 ? result.status : 1);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return result.stdout.replace(/[\r\n]+$/, '');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function printDoctor(activeProfile, cacheContentPath, cacheMetaPath) {
|
|
131
|
+
process.stdout.write(`profile=${activeProfile}\n`);
|
|
132
|
+
process.stdout.write(`plugin_path=${pluginPath}\n`);
|
|
133
|
+
process.stdout.write(`config_path=${configPath}\n`);
|
|
134
|
+
process.stdout.write(`cache_content_path=${cacheContentPath}\n`);
|
|
135
|
+
process.stdout.write(`cache_meta_path=${cacheMetaPath}\n`);
|
|
136
|
+
process.stdout.write(`plugin_exists=${fs.existsSync(pluginAbsPath) ? 'yes' : 'no'}\n`);
|
|
137
|
+
process.stdout.write(`config_exists=${fs.existsSync(configAbsPath) ? 'yes' : 'no'}\n`);
|
|
138
|
+
process.stdout.write(`cache_content_exists=${fs.existsSync(path.resolve(process.cwd(), cacheContentPath)) ? 'yes' : 'no'}\n`);
|
|
139
|
+
process.stdout.write(`cache_meta_exists=${fs.existsSync(path.resolve(process.cwd(), cacheMetaPath)) ? 'yes' : 'no'}\n`);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function resolveWindowsOpencodeInvocation() {
|
|
143
|
+
const pathValue = process.env.PATH || '';
|
|
144
|
+
const searchDirs = pathValue.split(path.delimiter).filter(Boolean);
|
|
145
|
+
|
|
146
|
+
for (const searchDir of searchDirs) {
|
|
147
|
+
const commandPath = path.join(searchDir, 'opencode.cmd');
|
|
148
|
+
if (!fs.existsSync(commandPath)) {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
const raw = fs.readFileSync(commandPath, 'utf8');
|
|
154
|
+
const matches = [...raw.matchAll(/"([^"]+\.(?:c|m)?js)"/gi)];
|
|
155
|
+
const scriptRef = matches.at(-1)?.[1];
|
|
156
|
+
|
|
157
|
+
if (!scriptRef) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const normalized = scriptRef
|
|
162
|
+
.replace(/%dp0%/gi, `${searchDir}${path.sep}`)
|
|
163
|
+
.replace(/\\/g, path.sep);
|
|
164
|
+
const resolvedScriptPath = path.resolve(normalized);
|
|
165
|
+
|
|
166
|
+
if (fs.existsSync(resolvedScriptPath)) {
|
|
167
|
+
return {
|
|
168
|
+
command: process.execPath,
|
|
169
|
+
argsPrefix: [resolvedScriptPath],
|
|
170
|
+
shell: false,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
} catch {
|
|
174
|
+
// ignore unreadable shims and continue searching PATH
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
command: 'opencode',
|
|
180
|
+
argsPrefix: [],
|
|
181
|
+
shell: true,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function spawnOpencode(args, extraEnv = {}) {
|
|
186
|
+
const invocation = process.platform === 'win32'
|
|
187
|
+
? resolveWindowsOpencodeInvocation()
|
|
188
|
+
: { command: 'opencode', argsPrefix: [], shell: false };
|
|
189
|
+
const child = spawn(invocation.command, [...invocation.argsPrefix, ...args], {
|
|
190
|
+
env: {
|
|
191
|
+
...process.env,
|
|
192
|
+
...extraEnv,
|
|
193
|
+
},
|
|
194
|
+
stdio: 'inherit',
|
|
195
|
+
shell: invocation.shell,
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
const signals = ['SIGINT', 'SIGTERM'];
|
|
199
|
+
if (process.platform !== 'win32') {
|
|
200
|
+
signals.push('SIGHUP');
|
|
201
|
+
} else {
|
|
202
|
+
signals.push('SIGBREAK');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
let finished = false;
|
|
206
|
+
const handlers = new Map();
|
|
207
|
+
|
|
208
|
+
const cleanup = () => {
|
|
209
|
+
for (const [signal, handler] of handlers) {
|
|
210
|
+
process.off(signal, handler);
|
|
211
|
+
}
|
|
212
|
+
handlers.clear();
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const finish = (code, signal) => {
|
|
216
|
+
if (finished) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
finished = true;
|
|
221
|
+
cleanup();
|
|
222
|
+
|
|
223
|
+
if (signal) {
|
|
224
|
+
const signalCode = osConstants.signals?.[signal] ?? signalExitCodes[signal];
|
|
225
|
+
process.exit(typeof signalCode === 'number' ? 128 + signalCode : 1);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
process.exit(code ?? 0);
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
for (const signal of signals) {
|
|
232
|
+
const handler = () => {
|
|
233
|
+
if (child.exitCode !== null || child.signalCode !== null) {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
try {
|
|
238
|
+
child.kill(signal);
|
|
239
|
+
} catch {
|
|
240
|
+
// ignore forwarding errors and wait for child exit
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
handlers.set(signal, handler);
|
|
245
|
+
process.on(signal, handler);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
child.on('error', (error) => {
|
|
249
|
+
cleanup();
|
|
250
|
+
console.error(`moc: failed to launch opencode: ${error.message}`);
|
|
251
|
+
process.exit(1);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
child.on('exit', finish);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function main() {
|
|
258
|
+
const originalArgs = process.argv.slice(2);
|
|
259
|
+
let forwardedArgs = [...originalArgs];
|
|
260
|
+
let profile = '';
|
|
261
|
+
let commandMode = '';
|
|
262
|
+
|
|
263
|
+
if (originalArgs[0] === 'doctor' || originalArgs[0] === 'print-config') {
|
|
264
|
+
commandMode = originalArgs[0];
|
|
265
|
+
const candidateProfile = originalArgs[1];
|
|
266
|
+
|
|
267
|
+
if (typeof candidateProfile === 'string' && candidateProfile.length > 0) {
|
|
268
|
+
profile = candidateProfile;
|
|
269
|
+
} else {
|
|
270
|
+
profile = 'opencode';
|
|
271
|
+
}
|
|
272
|
+
} else if (typeof originalArgs[0] === 'string' && isKnownProfile(originalArgs[0])) {
|
|
273
|
+
profile = originalArgs[0];
|
|
274
|
+
forwardedArgs = originalArgs.slice(1);
|
|
275
|
+
} else if (originalArgs.length === 0) {
|
|
276
|
+
const envProfile = typeof process.env.OPENCODE_PROVIDER_PROFILE === 'string'
|
|
277
|
+
? normalizeProfile(process.env.OPENCODE_PROVIDER_PROFILE)
|
|
278
|
+
: '';
|
|
279
|
+
|
|
280
|
+
profile = envProfile || 'opencode';
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (!profile) {
|
|
284
|
+
spawnOpencode(forwardedArgs);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (!isKnownProfile(profile)) {
|
|
289
|
+
console.error(`moc: unknown profile '${profile}'. Must be one of: ${knownProfilesText}`);
|
|
290
|
+
process.exit(1);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const cacheDir = path.join('.opencode', 'cache', 'provider-profile');
|
|
294
|
+
const cacheContentPath = path.join(cacheDir, `${profile}.json`);
|
|
295
|
+
const cacheMetaPath = path.join(cacheDir, `${profile}.meta`);
|
|
296
|
+
const cacheContentAbsPath = path.resolve(process.cwd(), cacheContentPath);
|
|
297
|
+
const cacheMetaAbsPath = path.resolve(process.cwd(), cacheMetaPath);
|
|
298
|
+
|
|
299
|
+
if (!fs.existsSync(pluginAbsPath)) {
|
|
300
|
+
console.error(`moc: plugin not found at ${pluginPath}; falling back to plain opencode`);
|
|
301
|
+
spawnOpencode(forwardedArgs);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if (!fs.existsSync(configAbsPath)) {
|
|
306
|
+
console.error(`moc: config not found at ${configPath}; cannot compute provider profile override`);
|
|
307
|
+
process.exit(1);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (commandMode === 'doctor') {
|
|
311
|
+
printDoctor(profile, cacheContentPath, cacheMetaPath);
|
|
312
|
+
process.exit(0);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const pluginMtime = getMtime(pluginAbsPath, 'plugin');
|
|
316
|
+
const configMtime = getMtime(configAbsPath, 'config');
|
|
317
|
+
|
|
318
|
+
let configContent = '';
|
|
319
|
+
const meta = readMeta(cacheMetaAbsPath);
|
|
320
|
+
|
|
321
|
+
if (meta && fs.existsSync(cacheContentAbsPath)) {
|
|
322
|
+
if (
|
|
323
|
+
meta.profile === profile
|
|
324
|
+
&& meta.plugin_mtime === pluginMtime
|
|
325
|
+
&& meta.config_mtime === configMtime
|
|
326
|
+
) {
|
|
327
|
+
configContent = readCacheContent(cacheContentAbsPath);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (!configContent) {
|
|
332
|
+
configContent = computeConfigContent(profile);
|
|
333
|
+
|
|
334
|
+
if (!writeCache(cacheDir, cacheContentAbsPath, cacheMetaAbsPath, profile, pluginMtime, configMtime, configContent)) {
|
|
335
|
+
console.error(`moc: warning: failed to update cache at ${cacheDir}; continuing with fresh config`);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (commandMode === 'print-config') {
|
|
340
|
+
process.stdout.write(`${configContent}\n`);
|
|
341
|
+
process.exit(0);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
spawnOpencode(forwardedArgs, {
|
|
345
|
+
OPENCODE_PROVIDER_PROFILE: profile,
|
|
346
|
+
OPENCODE_CONFIG_CONTENT: configContent,
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
main();
|
package/opencode.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://opencode.ai/config.json",
|
|
3
|
-
"model": "opencode/
|
|
4
|
-
"small_model": "opencode/
|
|
3
|
+
"model": "opencode/big-pickle",
|
|
4
|
+
"small_model": "opencode/deepseek-v4-flash-free",
|
|
5
5
|
"default_agent": "fullstack-dev",
|
|
6
6
|
"instructions": [
|
|
7
7
|
"skills/adr/SKILL.md",
|
|
@@ -11,11 +11,14 @@
|
|
|
11
11
|
"skills/docs-gen/SKILL.md",
|
|
12
12
|
"skills/orchestrate/SKILL.md"
|
|
13
13
|
],
|
|
14
|
+
"plugin": [
|
|
15
|
+
"plugins/provider-profile.mjs"
|
|
16
|
+
],
|
|
14
17
|
"agent": {
|
|
15
18
|
"fullstack-dev": {
|
|
16
19
|
"description": "Primary development agent. Orchestrates contract-first ADR → contract → code → verify flow.",
|
|
17
20
|
"mode": "primary",
|
|
18
|
-
|
|
21
|
+
"prompt": "{file:agents/fullstack-dev.md}",
|
|
19
22
|
"permission": {
|
|
20
23
|
"task": "ask"
|
|
21
24
|
},
|
|
@@ -30,7 +33,7 @@
|
|
|
30
33
|
"description": "Analyze requirements and produce contract specifications in docs/specs/. Writes persistent contracts for parallel development.",
|
|
31
34
|
"mode": "subagent",
|
|
32
35
|
"model": "opencode/big-pickle",
|
|
33
|
-
|
|
36
|
+
"prompt": "{file:agents/spec-writer.md}",
|
|
34
37
|
"tools": {
|
|
35
38
|
"read": true,
|
|
36
39
|
"bash": true,
|
|
@@ -41,7 +44,7 @@
|
|
|
41
44
|
"executor": {
|
|
42
45
|
"description": "Implement code changes following an approved contract. Full read/write access for coding.",
|
|
43
46
|
"mode": "subagent",
|
|
44
|
-
|
|
47
|
+
"prompt": "{file:agents/executor.md}",
|
|
45
48
|
"tools": {
|
|
46
49
|
"read": true,
|
|
47
50
|
"write": true,
|
|
@@ -52,8 +55,8 @@
|
|
|
52
55
|
"verifier": {
|
|
53
56
|
"description": "Run lint, typecheck, tests, and build to verify code changes. Read-only + bash.",
|
|
54
57
|
"mode": "subagent",
|
|
55
|
-
"model": "opencode/
|
|
56
|
-
|
|
58
|
+
"model": "opencode/deepseek-v4-flash-free",
|
|
59
|
+
"prompt": "{file:agents/verifier.md}",
|
|
57
60
|
"tools": {
|
|
58
61
|
"read": true,
|
|
59
62
|
"bash": true,
|
|
@@ -65,7 +68,7 @@
|
|
|
65
68
|
"description": "Systematic code review across correctness, security, quality, testing, and style dimensions.",
|
|
66
69
|
"mode": "subagent",
|
|
67
70
|
"model": "opencode/big-pickle",
|
|
68
|
-
|
|
71
|
+
"prompt": "{file:agents/code-reviewer.md}",
|
|
69
72
|
"tools": {
|
|
70
73
|
"read": true,
|
|
71
74
|
"bash": true,
|
|
@@ -76,7 +79,7 @@
|
|
|
76
79
|
"test-engineer": {
|
|
77
80
|
"description": "Generate meaningful tests for source code — unit, integration, and E2E. Framework-aware.",
|
|
78
81
|
"mode": "subagent",
|
|
79
|
-
|
|
82
|
+
"prompt": "{file:agents/test-engineer.md}",
|
|
80
83
|
"tools": {
|
|
81
84
|
"read": true,
|
|
82
85
|
"write": true,
|
|
@@ -87,7 +90,7 @@
|
|
|
87
90
|
"tech-writer": {
|
|
88
91
|
"description": "Generate and update project documentation — code comments, API docs, README, and CHANGELOG.",
|
|
89
92
|
"mode": "subagent",
|
|
90
|
-
|
|
93
|
+
"prompt": "{file:agents/tech-writer.md}",
|
|
91
94
|
"tools": {
|
|
92
95
|
"read": true,
|
|
93
96
|
"write": true,
|
|
@@ -99,7 +102,7 @@
|
|
|
99
102
|
"description": "Multi-agent orchestrator for complex tasks. Delegates to architect, spec-writer, executor, code-reviewer, test-engineer, verifier.",
|
|
100
103
|
"mode": "subagent",
|
|
101
104
|
"model": "opencode/big-pickle",
|
|
102
|
-
|
|
105
|
+
"prompt": "{file:agents/team-lead.md}",
|
|
103
106
|
"tools": {
|
|
104
107
|
"read": true,
|
|
105
108
|
"write": true,
|
|
@@ -111,7 +114,7 @@
|
|
|
111
114
|
"description": "Capture architecture decisions as ADR documents. Analyzes tradeoffs and records context.",
|
|
112
115
|
"mode": "subagent",
|
|
113
116
|
"model": "opencode/big-pickle",
|
|
114
|
-
|
|
117
|
+
"prompt": "{file:agents/architect.md}",
|
|
115
118
|
"tools": {
|
|
116
119
|
"read": true,
|
|
117
120
|
"write": true,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "my-opencode",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "高品質 opencode 開發流程 skill 與 agents 集合 — Contract-First 開發流程、ADR、PR Review、測試生成、文件生成",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"opencode",
|
|
@@ -29,13 +29,15 @@
|
|
|
29
29
|
"postinstall": "node scripts/postinstall.js"
|
|
30
30
|
},
|
|
31
31
|
"bin": {
|
|
32
|
-
"my-opencode": "bin/cli.js"
|
|
32
|
+
"my-opencode": "bin/cli.js",
|
|
33
|
+
"moc": "bin/moc.js"
|
|
33
34
|
},
|
|
34
35
|
"files": [
|
|
35
36
|
"bin/",
|
|
36
37
|
"scripts/",
|
|
37
38
|
"skills/",
|
|
38
39
|
"agents/",
|
|
40
|
+
"plugins/",
|
|
39
41
|
"opencode.json"
|
|
40
42
|
],
|
|
41
43
|
"engines": {
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# Plugins
|
|
2
|
+
|
|
3
|
+
opencode plugin 是透過 `opencode.json` 的 `plugin` 區塊註冊的 npm 或本地封裝,可包含自訂 tool、MCP 整合、或額外的 subagent 類型。
|
|
4
|
+
|
|
5
|
+
## 目前包含
|
|
6
|
+
|
|
7
|
+
### `provider-profile.mjs`
|
|
8
|
+
|
|
9
|
+
零依賴 ESM plugin / resolver,用來切換本 repo 的模型 profile。
|
|
10
|
+
|
|
11
|
+
- 預設 profile:`opencode`
|
|
12
|
+
- 支援:`opencode`、`openai`、`google`、`copilot`
|
|
13
|
+
- 讀取:`OPENCODE_PROVIDER_PROFILE`
|
|
14
|
+
- 可透過 plugin tuple options 覆寫:`["plugins/provider-profile.mjs", { "profile": "google" }]`
|
|
15
|
+
|
|
16
|
+
| Profile | Primary / heavy | Small / verifier |
|
|
17
|
+
|---------|------------------|------------------|
|
|
18
|
+
| `opencode` | `opencode/big-pickle` | `opencode/deepseek-v4-flash-free` |
|
|
19
|
+
| `openai` | `openai/gpt-5.4` | `openai/gpt-5.4-mini` |
|
|
20
|
+
| `google` | `google/gemini-3.1-pro-preview-customtools` | `google/gemini-3.5-flash` |
|
|
21
|
+
| `copilot` | `copilot/gpt-5.4` | `copilot/gpt-5.4-mini` |
|
|
22
|
+
|
|
23
|
+
### 用法
|
|
24
|
+
|
|
25
|
+
建議優先使用 `moc`:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
moc
|
|
29
|
+
moc openai
|
|
30
|
+
moc google --list-agents
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`moc` 會對四個已知 profile 快取 provider-profile 產生出的 JSON override,位置在 `.opencode/cache/provider-profile/`。快取是否有效由以下條件決定:
|
|
34
|
+
|
|
35
|
+
- profile 名稱
|
|
36
|
+
- `.opencode/plugins/provider-profile.mjs` 的 mtime
|
|
37
|
+
- `.opencode/opencode.json` 的 mtime
|
|
38
|
+
|
|
39
|
+
若三者都沒變,`moc` 會直接重用快取,不再重新執行 `node .opencode/plugins/provider-profile.mjs`。
|
|
40
|
+
|
|
41
|
+
進階情境可直接設定環境變數:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
OPENCODE_CONFIG_CONTENT="$(node .opencode/plugins/provider-profile.mjs)" opencode
|
|
45
|
+
|
|
46
|
+
OPENCODE_PROVIDER_PROFILE=openai \
|
|
47
|
+
OPENCODE_CONFIG_CONTENT="$(node .opencode/plugins/provider-profile.mjs)" \
|
|
48
|
+
opencode
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
直接執行模組時,會輸出可供 `OPENCODE_CONFIG_CONTENT` 使用的 JSON override。
|
|
52
|
+
|
|
53
|
+
> 備註:目前 OpenCode plugin API 可註冊 runtime hooks / tools,但不會在載入時直接改寫 `opencode.json`;因此這個模組同時扮演 plugin 與 config override generator。
|
|
54
|
+
|
|
55
|
+
## 何時需要 Plugin
|
|
56
|
+
|
|
57
|
+
- 需要封裝自訂 tool 給多個專案共用
|
|
58
|
+
- 需要透過 MCP 串接外部 API
|
|
59
|
+
- 需要 hook opencode 的生命週期事件
|
|
60
|
+
|
|
61
|
+
## 開發參考
|
|
62
|
+
|
|
63
|
+
- [opencode 官方文件 — Plugins](https://opencode.ai/plugins)
|
|
64
|
+
- 範本可參考 `templates/plugin-template/`
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = path.dirname(__filename);
|
|
7
|
+
const CONFIG_PATH = path.resolve(__dirname, "..", "opencode.json");
|
|
8
|
+
|
|
9
|
+
const PROFILE_MAP = Object.freeze({
|
|
10
|
+
opencode: Object.freeze({
|
|
11
|
+
model: "opencode/big-pickle",
|
|
12
|
+
small_model: "opencode/deepseek-v4-flash-free",
|
|
13
|
+
heavy_model: "opencode/big-pickle",
|
|
14
|
+
}),
|
|
15
|
+
openai: Object.freeze({
|
|
16
|
+
model: "openai/gpt-5.4",
|
|
17
|
+
small_model: "openai/gpt-5.4-mini",
|
|
18
|
+
heavy_model: "openai/gpt-5.4",
|
|
19
|
+
}),
|
|
20
|
+
google: Object.freeze({
|
|
21
|
+
model: "google/gemini-3.1-pro-preview-customtools",
|
|
22
|
+
small_model: "google/gemini-3.5-flash",
|
|
23
|
+
heavy_model: "google/gemini-3.1-pro-preview-customtools",
|
|
24
|
+
}),
|
|
25
|
+
copilot: Object.freeze({
|
|
26
|
+
model: "copilot/gpt-5.4",
|
|
27
|
+
small_model: "copilot/gpt-5.4-mini",
|
|
28
|
+
heavy_model: "copilot/gpt-5.4",
|
|
29
|
+
}),
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const SUPPORTED_PROFILES = Object.keys(PROFILE_MAP);
|
|
33
|
+
const HEAVY_AGENTS = ["spec-writer", "code-reviewer", "team-lead", "architect"];
|
|
34
|
+
const ALL_OVERRIDDEN_AGENTS = [...HEAVY_AGENTS, "verifier"];
|
|
35
|
+
|
|
36
|
+
function normalizeProfileValue(value) {
|
|
37
|
+
return typeof value === "string" ? value.trim() : "";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function assertKnownProfile(value) {
|
|
41
|
+
if (!SUPPORTED_PROFILES.includes(value)) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`Unknown provider profile "${value}". Must be one of: ${SUPPORTED_PROFILES.join(", ")}`,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function pickProfile(options = {}) {
|
|
49
|
+
const optionProfile = normalizeProfileValue(options.profile);
|
|
50
|
+
const envProfile = normalizeProfileValue(process.env.OPENCODE_PROVIDER_PROFILE);
|
|
51
|
+
const activeProfile = optionProfile || envProfile || "opencode";
|
|
52
|
+
|
|
53
|
+
assertKnownProfile(activeProfile);
|
|
54
|
+
|
|
55
|
+
return activeProfile;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export default function resolveProfile(options = {}) {
|
|
59
|
+
const activeProfile = pickProfile(options);
|
|
60
|
+
const profile = PROFILE_MAP[activeProfile];
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
model: profile.model,
|
|
64
|
+
small_model: profile.small_model,
|
|
65
|
+
agent_overrides: {
|
|
66
|
+
"spec-writer": profile.heavy_model,
|
|
67
|
+
verifier: profile.small_model,
|
|
68
|
+
"code-reviewer": profile.heavy_model,
|
|
69
|
+
"team-lead": profile.heavy_model,
|
|
70
|
+
architect: profile.heavy_model,
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function buildProfileConfig(options = {}) {
|
|
76
|
+
const resolved = resolveProfile(options);
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
model: resolved.model,
|
|
80
|
+
small_model: resolved.small_model,
|
|
81
|
+
agent: Object.fromEntries(
|
|
82
|
+
ALL_OVERRIDDEN_AGENTS.map((name) => [name, { model: resolved.agent_overrides[name] }]),
|
|
83
|
+
),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function isProviderProfilePlugin(entry) {
|
|
88
|
+
return typeof entry === "string" && entry.replaceAll("\\", "/").endsWith("provider-profile.mjs");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function readPluginOptions(configPath = CONFIG_PATH) {
|
|
92
|
+
try {
|
|
93
|
+
const config = JSON.parse(fs.readFileSync(configPath, "utf8"));
|
|
94
|
+
const plugins = Array.isArray(config.plugin) ? config.plugin : [];
|
|
95
|
+
|
|
96
|
+
for (const plugin of plugins) {
|
|
97
|
+
if (Array.isArray(plugin) && plugin.length === 2 && isProviderProfilePlugin(plugin[0])) {
|
|
98
|
+
return plugin[1] && typeof plugin[1] === "object" ? plugin[1] : {};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
} catch {
|
|
102
|
+
return {};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return {};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* ProviderProfilePlugin — runtime hook placeholder (no-op).
|
|
110
|
+
*
|
|
111
|
+
* This is a no-op runtime hook for opencode's plugin system.
|
|
112
|
+
* The actual model-profile switching happens outside the runtime hook:
|
|
113
|
+
*
|
|
114
|
+
* 1) `bin/moc` (the shell launcher) or direct invocation sets
|
|
115
|
+
* `OPENCODE_CONFIG_CONTENT` by running this module directly:
|
|
116
|
+
* `node plugins/provider-profile.mjs`
|
|
117
|
+
*
|
|
118
|
+
* 2) The direct-execution path at the bottom of this file generates
|
|
119
|
+
* the JSON config override fragment that opencode merges at startup.
|
|
120
|
+
*
|
|
121
|
+
* This function exists so the module is loadable as an opencode plugin,
|
|
122
|
+
* but no runtime transformation is needed because the config override is
|
|
123
|
+
* already applied via `OPENCODE_CONFIG_CONTENT` at process launch.
|
|
124
|
+
*
|
|
125
|
+
* @returns {object} Empty object — no runtime hook behavior.
|
|
126
|
+
*/
|
|
127
|
+
export function ProviderProfilePlugin() {
|
|
128
|
+
return {};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function isDirectExecution() {
|
|
132
|
+
const scriptPath = process.argv[1];
|
|
133
|
+
return scriptPath ? path.resolve(scriptPath) === __filename : false;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (isDirectExecution()) {
|
|
137
|
+
const options = readPluginOptions();
|
|
138
|
+
process.stdout.write(`${JSON.stringify(buildProfileConfig(options))}\n`);
|
|
139
|
+
}
|
package/scripts/postinstall.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* postinstall.js — npm postinstall hook
|
|
5
5
|
*
|
|
6
6
|
* After `npm install my-opencode` in another project, this script
|
|
7
|
-
* automatically copies agents/, skills/, and opencode.json to the
|
|
7
|
+
* automatically copies agents/, skills/, plugins/, and opencode.json to the
|
|
8
8
|
* target project's .opencode/ directory.
|
|
9
9
|
*
|
|
10
10
|
* The package stays in node_modules/ (gitignored, no residue).
|
|
@@ -24,6 +24,7 @@ const __dirname = path.dirname(__filename);
|
|
|
24
24
|
const PKG_DIR = path.resolve(__dirname, "..");
|
|
25
25
|
const SKILLS_SRC = path.join(PKG_DIR, "skills");
|
|
26
26
|
const AGENTS_SRC = path.join(PKG_DIR, "agents");
|
|
27
|
+
const PLUGINS_SRC = path.join(PKG_DIR, "plugins");
|
|
27
28
|
const CONFIG_SRC = path.join(PKG_DIR, "opencode.json");
|
|
28
29
|
|
|
29
30
|
// ─── Detect project root ────────────────────────────────────────────────
|
|
@@ -128,6 +129,7 @@ function main() {
|
|
|
128
129
|
const targetDir = path.join(projectRoot, ".opencode");
|
|
129
130
|
const targetSkills = path.join(targetDir, "skills");
|
|
130
131
|
const targetAgents = path.join(targetDir, "agents");
|
|
132
|
+
const targetPlugins = path.join(targetDir, "plugins");
|
|
131
133
|
const targetConfig = path.join(targetDir, "opencode.json");
|
|
132
134
|
|
|
133
135
|
// ── Create .opencode/ ──────────────────────────────────────────────────
|
|
@@ -168,15 +170,33 @@ function main() {
|
|
|
168
170
|
}
|
|
169
171
|
}
|
|
170
172
|
|
|
173
|
+
// ── Copy plugins ───────────────────────────────────────────────────────
|
|
174
|
+
let pluginCount = 0;
|
|
175
|
+
if (fs.existsSync(PLUGINS_SRC)) {
|
|
176
|
+
fs.mkdirSync(targetPlugins, { recursive: true });
|
|
177
|
+
const plugins = fs
|
|
178
|
+
.readdirSync(PLUGINS_SRC, { withFileTypes: true })
|
|
179
|
+
.filter((d) => d.isFile() && d.name.endsWith(".mjs"))
|
|
180
|
+
.map((d) => d.name);
|
|
181
|
+
|
|
182
|
+
for (const plugin of plugins) {
|
|
183
|
+
const dst = path.join(targetPlugins, plugin);
|
|
184
|
+
if (copyFileIfNotExists(path.join(PLUGINS_SRC, plugin), dst)) {
|
|
185
|
+
pluginCount++;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
171
190
|
// ── Copy opencode.json ────────────────────────────────────────────────
|
|
172
191
|
copyFileIfNotExists(CONFIG_SRC, targetConfig);
|
|
173
192
|
|
|
174
193
|
// Suppress output if nothing changed (quiet idempotency)
|
|
175
|
-
if (skillCount === 0 && agentCount === 0) return;
|
|
194
|
+
if (skillCount === 0 && agentCount === 0 && pluginCount === 0) return;
|
|
176
195
|
|
|
177
196
|
header("my-opencode: installed to .opencode/");
|
|
178
197
|
if (skillCount > 0) success(`Skills: ${skillCount}`);
|
|
179
198
|
if (agentCount > 0) success(`Agents: ${agentCount}`);
|
|
199
|
+
if (pluginCount > 0) success(`Plugins: ${pluginCount}`);
|
|
180
200
|
}
|
|
181
201
|
|
|
182
202
|
main();
|