Skip to content

Commit d636f08

Browse files
authored
feat: add OpenCode Claude Code auth (#249)
1 parent 3148263 commit d636f08

7 files changed

Lines changed: 744 additions & 10 deletions

File tree

lib/repair-opencode-json.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
--runtime <opencode|claude-code> \
4545
--chat-bridge <kimaki|cc-connect|telegram|none> \
4646
[--kimaki-plugins-dir <path>] \
47+
[--claude-code-auth-plugin <path>] \
4748
[--additive | --apply] \
4849
[--backup-suffix <timestamp>]
4950
@@ -77,6 +78,7 @@ def expected_plugins(
7778
runtime: str,
7879
chat_bridge: str,
7980
kimaki_plugins_dir: str,
81+
claude_code_auth_plugin: str = "",
8082
) -> List[str]:
8183
"""Return the `plugin` array wp-coding-agents setup would produce today.
8284
@@ -101,6 +103,9 @@ def expected_plugins(
101103
plugins.append(f"{kimaki_plugins_dir}/dm-context-filter.ts")
102104
plugins.append(f"{kimaki_plugins_dir}/dm-agent-sync.ts")
103105

106+
if claude_code_auth_plugin:
107+
plugins.append(claude_code_auth_plugin)
108+
104109
return plugins
105110

106111

@@ -361,6 +366,11 @@ def main() -> int:
361366
default="/opt/kimaki-config/plugins",
362367
help="Directory where DM plugins live (VPS default: /opt/kimaki-config/plugins)",
363368
)
369+
parser.add_argument(
370+
"--claude-code-auth-plugin",
371+
default="",
372+
help="Optional managed OpenCode Claude Code OAuth auth plugin path",
373+
)
364374
mode_group = parser.add_mutually_exclusive_group()
365375
mode_group.add_argument(
366376
"--apply",
@@ -426,6 +436,7 @@ def main() -> int:
426436
runtime=args.runtime,
427437
chat_bridge=args.chat_bridge,
428438
kimaki_plugins_dir=args.kimaki_plugins_dir.rstrip("/"),
439+
claude_code_auth_plugin=args.claude_code_auth_plugin,
429440
)
430441

431442
current: List[str] = list(data.get("plugin", []))

runtimes/opencode.sh

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,44 @@ runtime_install() {
1414
_opencode_register_runtime_signature
1515
}
1616

17+
opencode_claude_code_auth_enabled() {
18+
[ "${WITH_CLAUDE_CODE_AUTH:-false}" = true ] || return 1
19+
[ "${RUNTIME:-}" = "opencode" ] && return 0
20+
21+
local runtime
22+
for runtime in "${DETECTED_RUNTIMES[@]:-}"; do
23+
[ "$runtime" = "opencode" ] && return 0
24+
done
25+
return 1
26+
}
27+
28+
opencode_claude_code_auth_plugins_dir() {
29+
printf '%s/.opencode/plugins' "$SITE_PATH"
30+
}
31+
32+
opencode_claude_code_auth_plugin_path() {
33+
printf '%s/claude-code-auth.ts' "$(opencode_claude_code_auth_plugins_dir)"
34+
}
35+
36+
opencode_install_claude_code_auth_plugin() {
37+
opencode_claude_code_auth_enabled || return 0
38+
39+
local plugins_dir plugin_path source_path
40+
plugins_dir="$(opencode_claude_code_auth_plugins_dir)"
41+
plugin_path="$(opencode_claude_code_auth_plugin_path)"
42+
source_path="$SCRIPT_DIR/runtimes/opencode/plugins/claude-code-auth.ts"
43+
44+
if [ "$DRY_RUN" = true ]; then
45+
echo -e "${BLUE}[dry-run]${NC} Would install Claude Code auth OpenCode plugin at $plugin_path"
46+
return 0
47+
fi
48+
49+
mkdir -p "$plugins_dir"
50+
cp "$source_path" "$plugin_path"
51+
chmod 644 "$plugin_path"
52+
UPDATED_ITEMS+=("OpenCode Claude Code auth plugin ($plugin_path)")
53+
}
54+
1755
# _opencode_register_runtime_signature
1856
#
1957
# Publish opencode's worktree session-attribution env-var contract for the
@@ -169,6 +207,8 @@ runtime_generate_config() {
169207
fi
170208
fi
171209

210+
opencode_install_claude_code_auth_plugin
211+
172212
# On existing opencode.json, delegate to the repair helper in --additive
173213
# mode. This adds managed plugin entries the user is missing and applies
174214
# the prompt → instructions migration (fixes Anthropic Claude Max OAuth,
@@ -191,18 +231,17 @@ runtime_generate_config() {
191231
OPENCODE_JSON="$OPENCODE_JSON,\n \"small_model\": \"${OPENCODE_SMALL_MODEL}\""
192232
fi
193233

194-
# OpenCode plugins. wp-coding-agents only manages plugins it owns end to
195-
# end: dm-context-filter.ts and dm-agent-sync.ts on Kimaki bridges. The sync
196-
# plugin refreshes composed memory files without mutating config.agent slots.
197-
# opencode-claude-auth plugin is intentionally NOT installed on any bridge:
198-
# Kimaki ships a built-in AnthropicAuthPlugin and non-kimaki bridges use
199-
# opencode's native auth flow (`opencode auth login anthropic`). See
200-
# Extra-Chill/wp-coding-agents#117.
234+
# OpenCode plugins. The Data Machine prompt/memory plugins are managed on
235+
# Kimaki bridges. Claude Code OAuth auth is an explicit OpenCode runtime
236+
# opt-in so direct opencode sessions can use Claude Pro/Max subscription auth.
201237
OPENCODE_PLUGINS=""
202238
if [ "$CHAT_BRIDGE" = "kimaki" ]; then
203239
OPENCODE_PLUGINS="${OPENCODE_PLUGINS}\n \"${KIMAKI_PLUGINS_DIR}/dm-context-filter.ts\","
204240
OPENCODE_PLUGINS="${OPENCODE_PLUGINS}\n \"${KIMAKI_PLUGINS_DIR}/dm-agent-sync.ts\","
205241
fi
242+
if opencode_claude_code_auth_enabled; then
243+
OPENCODE_PLUGINS="${OPENCODE_PLUGINS}\n \"$(opencode_claude_code_auth_plugin_path)\","
244+
fi
206245

207246
if [ -n "$OPENCODE_PLUGINS" ]; then
208247
# Remove trailing comma from last plugin entry
@@ -273,6 +312,8 @@ _runtime_repair_opencode_json_additive() {
273312

274313
local BRIDGE_ARG="${CHAT_BRIDGE:-none}"
275314
local PLUGINS_DIR="${KIMAKI_PLUGINS_DIR:-/opt/kimaki-config/plugins}"
315+
local CLAUDE_CODE_AUTH_PLUGIN=""
316+
local claude_code_auth_args=()
276317
local SUFFIX
277318
SUFFIX="$(date +%Y%m%d-%H%M%S)"
278319
local MANAGED_INSTRUCTIONS_FILE=""
@@ -282,6 +323,10 @@ _runtime_repair_opencode_json_additive() {
282323
printf '%s\n' "$DM_AGENT_FILES" > "$MANAGED_INSTRUCTIONS_FILE"
283324
managed_args=(--managed-instructions-file "$MANAGED_INSTRUCTIONS_FILE")
284325
fi
326+
if opencode_claude_code_auth_enabled; then
327+
CLAUDE_CODE_AUTH_PLUGIN="$(opencode_claude_code_auth_plugin_path)"
328+
claude_code_auth_args=(--claude-code-auth-plugin "$CLAUDE_CODE_AUTH_PLUGIN")
329+
fi
285330

286331
log "opencode.json already exists — running additive repair..."
287332

@@ -291,6 +336,7 @@ _runtime_repair_opencode_json_additive() {
291336
--runtime opencode \
292337
--chat-bridge "$BRIDGE_ARG" \
293338
--kimaki-plugins-dir "$PLUGINS_DIR" \
339+
"${claude_code_auth_args[@]}" \
294340
"${managed_args[@]}" \
295341
--additive \
296342
--backup-suffix "$SUFFIX" 2>&1) && repair_rc=0 || repair_rc=$?

0 commit comments

Comments
 (0)