|
| 1 | +import type { TemplateConfig } from "../domain.js" |
| 2 | + |
| 3 | +const entrypointOpenCodeTemplate = `OPENCODE_DATA_DIR="/home/__SSH_USER__/.local/share/opencode" |
| 4 | +OPENCODE_AUTH_FILE="$OPENCODE_DATA_DIR/auth.json" |
| 5 | +OPENCODE_SHARED_HOME="__CODEX_HOME__-shared/opencode" |
| 6 | +OPENCODE_SHARED_AUTH_FILE="$OPENCODE_SHARED_HOME/auth.json" |
| 7 | +
|
| 8 | +# OpenCode: share auth.json across projects (so /connect is one-time) |
| 9 | +OPENCODE_SHARE_AUTH="\${OPENCODE_SHARE_AUTH:-1}" |
| 10 | +if [[ "$OPENCODE_SHARE_AUTH" == "1" ]]; then |
| 11 | + # Store in the shared auth volume to persist across projects/containers. |
| 12 | + mkdir -p "$OPENCODE_DATA_DIR" "$OPENCODE_SHARED_HOME" |
| 13 | + chown -R 1000:1000 "$OPENCODE_DATA_DIR" "$OPENCODE_SHARED_HOME" || true |
| 14 | +
|
| 15 | + # Guard against a bad bind mount creating a directory at auth.json. |
| 16 | + if [[ -d "$OPENCODE_AUTH_FILE" ]]; then |
| 17 | + mv "$OPENCODE_AUTH_FILE" "$OPENCODE_AUTH_FILE.bak-$(date +%s)" || true |
| 18 | + fi |
| 19 | +
|
| 20 | + # Migrate existing per-project auth into the shared location once. |
| 21 | + if [[ -f "$OPENCODE_AUTH_FILE" && ! -L "$OPENCODE_AUTH_FILE" ]]; then |
| 22 | + if [[ -f "$OPENCODE_SHARED_AUTH_FILE" ]]; then |
| 23 | + LOCAL_AUTH="$OPENCODE_AUTH_FILE" SHARED_AUTH="$OPENCODE_SHARED_AUTH_FILE" node - <<'NODE' |
| 24 | +const fs = require("fs") |
| 25 | +const localPath = process.env.LOCAL_AUTH |
| 26 | +const sharedPath = process.env.SHARED_AUTH |
| 27 | +const readJson = (p) => { |
| 28 | + try { |
| 29 | + return JSON.parse(fs.readFileSync(p, "utf8")) |
| 30 | + } catch { |
| 31 | + return {} |
| 32 | + } |
| 33 | +} |
| 34 | +const local = readJson(localPath) |
| 35 | +const shared = readJson(sharedPath) |
| 36 | +const merged = { ...local, ...shared } // shared wins on conflicts |
| 37 | +fs.writeFileSync(sharedPath, JSON.stringify(merged, null, 2), { mode: 0o600 }) |
| 38 | +NODE |
| 39 | + else |
| 40 | + cp "$OPENCODE_AUTH_FILE" "$OPENCODE_SHARED_AUTH_FILE" || true |
| 41 | + chmod 600 "$OPENCODE_SHARED_AUTH_FILE" || true |
| 42 | + fi |
| 43 | + chown 1000:1000 "$OPENCODE_SHARED_AUTH_FILE" || true |
| 44 | + rm -f "$OPENCODE_AUTH_FILE" || true |
| 45 | + fi |
| 46 | +
|
| 47 | + ln -sf "$OPENCODE_SHARED_AUTH_FILE" "$OPENCODE_AUTH_FILE" |
| 48 | +fi |
| 49 | +
|
| 50 | +# OpenCode: auto-seed auth from Codex (so /connect is automatic) |
| 51 | +OPENCODE_AUTO_CONNECT="\${OPENCODE_AUTO_CONNECT:-1}" |
| 52 | +if [[ "$OPENCODE_AUTO_CONNECT" == "1" ]]; then |
| 53 | + CODEX_AUTH_FILE="__CODEX_HOME__/auth.json" |
| 54 | + OPENCODE_SEED_AUTH="$OPENCODE_AUTH_FILE" |
| 55 | + if [[ "$OPENCODE_SHARE_AUTH" == "1" ]]; then |
| 56 | + OPENCODE_SEED_AUTH="$OPENCODE_SHARED_AUTH_FILE" |
| 57 | + fi |
| 58 | + CODEX_AUTH="$CODEX_AUTH_FILE" OPENCODE_AUTH="$OPENCODE_SEED_AUTH" node - <<'NODE' |
| 59 | +const fs = require("fs") |
| 60 | +const path = require("path") |
| 61 | +
|
| 62 | +const codexPath = process.env.CODEX_AUTH |
| 63 | +const opencodePath = process.env.OPENCODE_AUTH |
| 64 | +
|
| 65 | +if (!codexPath || !opencodePath) { |
| 66 | + process.exit(0) |
| 67 | +} |
| 68 | +
|
| 69 | +const readJson = (p) => { |
| 70 | + try { |
| 71 | + return JSON.parse(fs.readFileSync(p, "utf8")) |
| 72 | + } catch { |
| 73 | + return undefined |
| 74 | + } |
| 75 | +} |
| 76 | +
|
| 77 | +const writeJsonAtomic = (p, value) => { |
| 78 | + const dir = path.dirname(p) |
| 79 | + fs.mkdirSync(dir, { recursive: true }) |
| 80 | + const tmp = path.join(dir, ".tmp-" + path.basename(p) + "-" + process.pid + "-" + Date.now()) |
| 81 | + fs.writeFileSync(tmp, JSON.stringify(value, null, 2), { mode: 0o600 }) |
| 82 | + fs.renameSync(tmp, p) |
| 83 | +} |
| 84 | +
|
| 85 | +const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value) |
| 86 | +
|
| 87 | +const decodeJwtClaims = (jwt) => { |
| 88 | + if (typeof jwt !== "string") return undefined |
| 89 | + const parts = jwt.split(".") |
| 90 | + if (parts.length !== 3) return undefined |
| 91 | + try { |
| 92 | + const payload = Buffer.from(parts[1], "base64url").toString("utf8") |
| 93 | + return JSON.parse(payload) |
| 94 | + } catch { |
| 95 | + return undefined |
| 96 | + } |
| 97 | +} |
| 98 | +
|
| 99 | +const extractAccountIdFromClaims = (claims) => { |
| 100 | + if (!isRecord(claims)) return undefined |
| 101 | + if (typeof claims.chatgpt_account_id === "string") return claims.chatgpt_account_id |
| 102 | + const openaiAuth = claims["https://api.openai.com/auth"] |
| 103 | + if (isRecord(openaiAuth) && typeof openaiAuth.chatgpt_account_id === "string") { |
| 104 | + return openaiAuth.chatgpt_account_id |
| 105 | + } |
| 106 | + const orgs = claims.organizations |
| 107 | + if (Array.isArray(orgs) && orgs.length > 0) { |
| 108 | + const first = orgs[0] |
| 109 | + if (isRecord(first) && typeof first.id === "string") return first.id |
| 110 | + } |
| 111 | + return undefined |
| 112 | +} |
| 113 | +
|
| 114 | +const extractJwtExpiryMs = (claims) => { |
| 115 | + if (!isRecord(claims)) return undefined |
| 116 | + if (typeof claims.exp !== "number") return undefined |
| 117 | + return claims.exp * 1000 |
| 118 | +} |
| 119 | +
|
| 120 | +const codex = readJson(codexPath) |
| 121 | +if (!isRecord(codex)) process.exit(0) |
| 122 | +
|
| 123 | +let opencode = readJson(opencodePath) |
| 124 | +if (!isRecord(opencode)) opencode = {} |
| 125 | +
|
| 126 | +if (opencode.openai) { |
| 127 | + process.exit(0) |
| 128 | +} |
| 129 | +
|
| 130 | +const apiKey = codex.OPENAI_API_KEY |
| 131 | +if (typeof apiKey === "string" && apiKey.trim().length > 0) { |
| 132 | + opencode.openai = { type: "api", key: apiKey.trim() } |
| 133 | + writeJsonAtomic(opencodePath, opencode) |
| 134 | + process.exit(0) |
| 135 | +} |
| 136 | +
|
| 137 | +const tokens = codex.tokens |
| 138 | +if (!isRecord(tokens)) process.exit(0) |
| 139 | +
|
| 140 | +const access = tokens.access_token |
| 141 | +const refresh = tokens.refresh_token |
| 142 | +if (typeof access !== "string" || access.length === 0) process.exit(0) |
| 143 | +if (typeof refresh !== "string" || refresh.length === 0) process.exit(0) |
| 144 | +
|
| 145 | +const accessClaims = decodeJwtClaims(access) |
| 146 | +const expires = extractJwtExpiryMs(accessClaims) |
| 147 | +if (typeof expires !== "number") process.exit(0) |
| 148 | +
|
| 149 | +let accountId = undefined |
| 150 | +if (typeof tokens.account_id === "string" && tokens.account_id.length > 0) { |
| 151 | + accountId = tokens.account_id |
| 152 | +} else { |
| 153 | + const idClaims = decodeJwtClaims(tokens.id_token) |
| 154 | + accountId = |
| 155 | + extractAccountIdFromClaims(idClaims) || |
| 156 | + extractAccountIdFromClaims(accessClaims) |
| 157 | +} |
| 158 | +
|
| 159 | +const entry = { |
| 160 | + type: "oauth", |
| 161 | + refresh, |
| 162 | + access, |
| 163 | + expires, |
| 164 | + ...(typeof accountId === "string" && accountId.length > 0 ? { accountId } : {}) |
| 165 | +} |
| 166 | +
|
| 167 | +opencode.openai = entry |
| 168 | +writeJsonAtomic(opencodePath, opencode) |
| 169 | +NODE |
| 170 | + chown 1000:1000 "$OPENCODE_SEED_AUTH" 2>/dev/null || true |
| 171 | +fi |
| 172 | +
|
| 173 | +# OpenCode: ensure global config exists (plugins + permissions) |
| 174 | +OPENCODE_CONFIG_DIR="/home/__SSH_USER__/.config/opencode" |
| 175 | +OPENCODE_CONFIG_JSON="$OPENCODE_CONFIG_DIR/opencode.json" |
| 176 | +OPENCODE_CONFIG_JSONC="$OPENCODE_CONFIG_DIR/opencode.jsonc" |
| 177 | +
|
| 178 | +mkdir -p "$OPENCODE_CONFIG_DIR" |
| 179 | +chown -R 1000:1000 "$OPENCODE_CONFIG_DIR" || true |
| 180 | +
|
| 181 | +if [[ ! -f "$OPENCODE_CONFIG_JSON" && ! -f "$OPENCODE_CONFIG_JSONC" ]]; then |
| 182 | + cat <<'EOF' > "$OPENCODE_CONFIG_JSON" |
| 183 | +{ |
| 184 | + "$schema": "https://opencode.ai/config.json", |
| 185 | + "plugin": ["oh-my-opencode"], |
| 186 | + "permission": { |
| 187 | + "doom_loop": "allow", |
| 188 | + "external_directory": "allow", |
| 189 | + "read": { |
| 190 | + "*": "allow", |
| 191 | + "*.env": "allow", |
| 192 | + "*.env.*": "allow", |
| 193 | + "*.env.example": "allow" |
| 194 | + } |
| 195 | + } |
| 196 | +} |
| 197 | +EOF |
| 198 | + chown 1000:1000 "$OPENCODE_CONFIG_JSON" || true |
| 199 | +fi` |
| 200 | + |
| 201 | +// CHANGE: bootstrap OpenCode config (permissions + plugins) and share OpenCode auth.json across projects |
| 202 | +// WHY: make OpenCode usable out-of-the-box inside disposable docker-git containers |
| 203 | +// QUOTE(ТЗ): "Preinstall OpenCode and oh-my-opencode with full authorization of existing tools" |
| 204 | +// REF: issue-34 |
| 205 | +// SOURCE: n/a |
| 206 | +// FORMAT THEOREM: forall s: start(s) -> config_exists(s) |
| 207 | +// PURITY: CORE |
| 208 | +// INVARIANT: never overwrites an existing opencode.json/opencode.jsonc |
| 209 | +// COMPLEXITY: O(1) |
| 210 | +export const renderEntrypointOpenCodeConfig = (config: TemplateConfig): string => |
| 211 | + entrypointOpenCodeTemplate |
| 212 | + .replaceAll("__SSH_USER__", config.sshUser) |
| 213 | + .replaceAll("__CODEX_HOME__", config.codexHome) |
0 commit comments