Something went wrong. Try again.
opencode v2 mirror
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138export * as RemoteCli from "./remote-cli"
import { Effect, Schema } from "effect"import { HttpClient, HttpClientResponse } from "effect/http"
export class Failure extends Schema.TaggedError<Failure>()("RemoteCliFailure", { code: Schema.Literals(["platform", "version", "install"]), detail: Schema.String,}) { override get message() { return this.detail }}
export function quote(value: string) { return `'${value.replaceAll("'", "'\\''")}'`}
export function requireVersion(version: string) { if (version !== "local" && !/^[0-9][a-zA-Z0-9.+-]*$/.test(version)) throw new Failure({ code: "version", detail: version })
return version}
export function discoverScript(options: { fromPath?: boolean; cache?: { directory: string; prefix: string } } = {}) { return `cli=${options.fromPath ? "$(command -v opencode || true)" : '""'}if [ -z "$cli" ] && [ -x "$HOME/.opencode/bin/opencode" ]; then cli="$HOME/.opencode/bin/opencode"; fi${ options.cache ? `if [ -z "$cli" ]; then for binary in "$HOME"/${quote(options.cache.directory)}/${quote(options.cache.prefix)}*/opencode; do if [ -x "$binary" ]; then cli="$binary"; fi donefi` : ""}if [ -n "$cli" ]; then printf '%s\\n' "$cli"; fi`}
// Adapters supply a quoted shell expression, including remote HOME or wslpath expansion.export function versionScript(command: string) { return `if [ -x ${command} ]; then ${command} --version 2>/dev/null || true; fi\n`}
export function parseVersion(output: string) { const line = output .split(/\r?\n/) .find((line) => line.trim()) ?.trim()
if (!line) return null const marker = line.lastIndexOf(" v") const version = marker === -1 ? line : line.slice(marker + 2)
if (!version) throw new Error("V2 CLI did not provide a version")
return version}
export const probeScript = `set -euos=$(uname -s | tr '[:upper:]' '[:lower:]')arch=$(uname -m)case "$os" in linux|darwin) ;; *) exit 2 ;; esaccase "$arch" in x86_64|amd64) arch=x64 ;; aarch64|arm64) arch=arm64 ;; *) exit 2 ;; esactarget="$os-$arch"if [ "$arch" = x64 ]; then target="$target-baseline"; fiif [ "$os" = linux ]; then if [ -f /etc/alpine-release ] || (ldd --version 2>&1 | grep -qi musl); then target="$target-musl"; fifiprintf 'OPENCODE_REMOTE_TARGET=%s\\n' "$target"`
export function archiveUrl(target: string, version: string) { if (!/^(linux|darwin)-(x64-baseline|arm64)(-musl)?$/.test(target)) throw new Failure({ code: "platform", detail: target })
return `https://registry.npmjs.org/@opencode/cli-${target}/-/cli-${target}-${requireVersion(version)}.tgz`}
type Source = { type: "download"; url: string } | { type: "archive" } | { type: "installer"; binary?: string }
export function installScript(input: { version: string; directory?: string; source: Source }) { const version = requireVersion(input.version)
// The managed CLI installer also configures the user's shell PATH. Private // installations use archives so their destination and shell setup stay isolated. if (input.source.type === "installer") return `set -eucurl -fsSL https://raw.githubusercontent.com/anomalyco/opencode/v2/install | bash -s -- ${input.source.binary ? `--binary ${input.source.binary}` : `--version ${quote(version)}`}${verifyScript('"$HOME/.opencode/bin/opencode"', version)}`
return `set -euumask 077destination="$HOME"/${quote(`${input.directory ?? ".opencode/bin"}/opencode`)}mkdir -p "$(dirname "$destination")"stage=$(mktemp -d "$(dirname "$destination")/.install-XXXXXX")trap 'rm -rf "$stage"' EXIT${stageBinary(input.source)}chmod 755 "$stage/package/bin/opencode"${verifyScript('"$stage/package/bin/opencode"', version)}mv "$stage/package/bin/opencode" "$destination"`}
function stageBinary(source: Exclude<Source, { type: "installer" }>) { if (source.type === "archive") return 'cat > "$stage/archive.tgz"\ntar -xzf "$stage/archive.tgz" -C "$stage"'
return `url=${quote(source.url)}if command -v curl >/dev/null 2>&1; then curl -fsSL --connect-timeout 15 --max-time 180 "$url" -o "$stage/archive.tgz"else wget -T 180 -O "$stage/archive.tgz" "$url"fitar -xzf "$stage/archive.tgz" -C "$stage"`}
function verifyScript(command: string, version: string) { return `test "$(${command} --version | awk '{print $NF}' | sed 's/^v//')" = ${quote(version)}`}
const Beta = Schema.Struct({ version: Schema.String.check(Schema.isPattern(/^0\.0\.0-beta-\d+(?:\.\d+)?$/)) })
export const latestBeta = Effect.fn("RemoteCli.latestBeta")(function* () { const http = yield* HttpClient.HttpClient
const metadata = yield* http.get("https://registry.npmjs.org/@opencode%2fcli/beta").pipe( Effect.flatMap(HttpClientResponse.filterStatusOk), Effect.flatMap(HttpClientResponse.schemaBodyJson(Beta)), Effect.timeout("30 seconds"), Effect.mapError(() => new Failure({ code: "install", detail: "https://registry.npmjs.org/@opencode%2fcli/beta" })), )
return metadata.version})