Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 180 additions & 0 deletions packages/opencode/bin/altimate-code
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
#!/usr/bin/env node

const childProcess = require("child_process")
const fs = require("fs")
const path = require("path")
const os = require("os")

function run(target) {
const result = childProcess.spawnSync(target, process.argv.slice(2), {
stdio: "inherit",
})
if (result.error) {
console.error(result.error.message)
process.exit(1)
}
const code = typeof result.status === "number" ? result.status : 0
process.exit(code)
}

const envPath = process.env.ALTIMATE_CODE_BIN_PATH
if (envPath) {
run(envPath)
}

const scriptPath = fs.realpathSync(__filename)
const scriptDir = path.dirname(scriptPath)

//
const cached = path.join(scriptDir, ".altimate-code")
if (fs.existsSync(cached)) {
run(cached)
}

const platformMap = {
darwin: "darwin",
linux: "linux",
win32: "windows",
}
const archMap = {
x64: "x64",
arm64: "arm64",
arm: "arm",
}

let platform = platformMap[os.platform()]
if (!platform) {
platform = os.platform()
}
let arch = archMap[os.arch()]
if (!arch) {
arch = os.arch()
}
const scope = "@altimateai"
const base = "altimate-code-" + platform + "-" + arch
const binary = platform === "windows" ? "altimate-code.exe" : "altimate-code"

function supportsAvx2() {
if (arch !== "x64") return false

if (platform === "linux") {
try {
return /(^|\s)avx2(\s|$)/i.test(fs.readFileSync("/proc/cpuinfo", "utf8"))
} catch {
return false
}
}

if (platform === "darwin") {
try {
const result = childProcess.spawnSync("sysctl", ["-n", "hw.optional.avx2_0"], {
encoding: "utf8",
timeout: 1500,
})
if (result.status !== 0) return false
return (result.stdout || "").trim() === "1"
} catch {
return false
}
}

if (platform === "windows") {
const cmd =
'(Add-Type -MemberDefinition "[DllImport(""kernel32.dll"")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);" -Name Kernel32 -Namespace Win32 -PassThru)::IsProcessorFeaturePresent(40)'

for (const exe of ["powershell.exe", "pwsh.exe", "pwsh", "powershell"]) {
try {
const result = childProcess.spawnSync(exe, ["-NoProfile", "-NonInteractive", "-Command", cmd], {
encoding: "utf8",
timeout: 3000,
windowsHide: true,
})
if (result.status !== 0) continue
const out = (result.stdout || "").trim().toLowerCase()
if (out === "true" || out === "1") return true
if (out === "false" || out === "0") return false
} catch {
continue
}
}

return false
}

return false
}

const names = (() => {
const avx2 = supportsAvx2()
const baseline = arch === "x64" && !avx2

if (platform === "linux") {
const musl = (() => {
try {
if (fs.existsSync("/etc/alpine-release")) return true
} catch {
// ignore
}

try {
const result = childProcess.spawnSync("ldd", ["--version"], { encoding: "utf8" })
const text = ((result.stdout || "") + (result.stderr || "")).toLowerCase()
if (text.includes("musl")) return true
} catch {
// ignore
}

return false
})()

if (musl) {
if (arch === "x64") {
if (baseline) return [`${base}-baseline-musl`, `${base}-musl`, `${base}-baseline`, base]
return [`${base}-musl`, `${base}-baseline-musl`, base, `${base}-baseline`]
}
return [`${base}-musl`, base]
}

if (arch === "x64") {
if (baseline) return [`${base}-baseline`, base, `${base}-baseline-musl`, `${base}-musl`]
return [base, `${base}-baseline`, `${base}-musl`, `${base}-baseline-musl`]
}
return [base, `${base}-musl`]
}

if (arch === "x64") {
if (baseline) return [`${base}-baseline`, base]
return [base, `${base}-baseline`]
}
return [base]
})()

function findBinary(startDir) {
let current = startDir
for (;;) {
const modules = path.join(current, "node_modules")
if (fs.existsSync(modules)) {
for (const name of names) {
const candidate = path.join(modules, scope, name, "bin", binary)
if (fs.existsSync(candidate)) return candidate
}
}
const parent = path.dirname(current)
if (parent === current) {
return
}
current = parent
}
}

const resolved = findBinary(scriptDir)
if (!resolved) {
console.error(
"It seems that your package manager failed to install the right version of the altimate-code CLI for your platform. You can try manually installing " +
names.map((n) => `\"${scope}/${n}\"`).join(" or ") +
" package",
)
process.exit(1)
}

run(resolved)
2 changes: 1 addition & 1 deletion packages/opencode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"bin": {
"opencode": "./bin/opencode",
"altimate": "./bin/altimate",
"altimate-code": "./bin/altimate"
"altimate-code": "./bin/altimate-code"
},
"exports": {
"./*": "./src/*.ts"
Expand Down
43 changes: 43 additions & 0 deletions packages/opencode/src/acp/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export namespace ACP {
private eventAbort = new AbortController()
private eventStarted = false
private bashSnapshots = new Map<string, string>()
private pendingEmitted = new Set<string>()
private permissionQueues = new Map<string, Promise<void>>()
private permissionOptions: PermissionOption[] = [
{ optionId: "once", kind: "allow_once", name: "Allow once" },
Expand Down Expand Up @@ -290,6 +291,8 @@ export namespace ACP {
if (part.type === "tool") {
switch (part.state.status) {
case "pending":
this.bashSnapshots.delete(part.callID)
this.pendingEmitted.add(part.callID)
await this.connection
.sessionUpdate({
sessionId,
Expand All @@ -309,6 +312,25 @@ export namespace ACP {
return

case "running":
if (!this.pendingEmitted.has(part.callID)) {
this.pendingEmitted.add(part.callID)
await this.connection
.sessionUpdate({
sessionId,
update: {
sessionUpdate: "tool_call",
toolCallId: part.callID,
title: part.tool,
kind: toToolKind(part.tool),
status: "pending",
locations: [],
rawInput: {},
},
})
.catch((error) => {
log.error("failed to send synthetic tool pending to ACP", { error })
})
}
const output = this.bashOutput(part)
const content: ToolCallContent[] = []
if (output) {
Expand Down Expand Up @@ -354,6 +376,7 @@ export namespace ACP {
title: part.tool,
locations: toLocations(part.tool, part.state.input),
rawInput: part.state.input,
...(content.length > 0 && { content }),
},
})
.catch((error) => {
Expand Down Expand Up @@ -838,6 +861,7 @@ export namespace ACP {
if (part.type === "tool") {
switch (part.state.status) {
case "pending":
this.pendingEmitted.add(part.callID)
await this.connection
.sessionUpdate({
sessionId,
Expand All @@ -856,6 +880,25 @@ export namespace ACP {
})
break
case "running":
if (!this.pendingEmitted.has(part.callID)) {
this.pendingEmitted.add(part.callID)
await this.connection
.sessionUpdate({
sessionId,
update: {
sessionUpdate: "tool_call",
toolCallId: part.callID,
title: part.tool,
kind: toToolKind(part.tool),
status: "pending",
locations: [],
rawInput: {},
},
})
.catch((err) => {
log.error("failed to send synthetic tool pending to ACP", { error: err })
})
}
await this.connection
.sessionUpdate({
sessionId,
Expand Down
2 changes: 2 additions & 0 deletions packages/opencode/src/provider/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export namespace ProviderError {
/exceeded model token limit/i, // Kimi For Coding, Moonshot
/context[_ ]length[_ ]exceeded/i, // Generic fallback
/request entity too large/i, // HTTP 413
/the request was too long/i, // Azure OpenAI
/maximum tokens for requested operation/i, // Azure OpenAI
]

function isOpenAiErrorRetryable(e: APICallError) {
Expand Down
Loading
Loading