Skip to content

Commit 2356014

Browse files
committed
fix(provider): auto-refresh AWS SSO credentials on expiry
When fromNodeProviderChain throws CredentialsProviderError (expired or uninitialized SSO session), spawn 'aws sso login [--profile <name>]', await successful exit, then retry credential resolution transparently. The AI SDK never sees an error and the session continues uninterrupted. Detects by error .name (stable SDK contract) not message string.
1 parent f963b0a commit 2356014

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

packages/opencode/src/provider/provider.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import z from "zod"
22
import os from "os"
3+
import { spawn } from "child_process"
34
import fuzzysort from "fuzzysort"
45
import { Config } from "../config"
56
import { mapValues, mergeDeep, omit, pickBy, sortBy } from "remeda"
@@ -291,7 +292,23 @@ function custom(dep: CustomDep): Record<string, CustomLoader> {
291292
// Build credential provider options (only pass profile if specified)
292293
const credentialProviderOptions = profile ? { profile } : {}
293294

294-
providerOptions.credentialProvider = fromNodeProviderChain(credentialProviderOptions)
295+
const rawProvider = fromNodeProviderChain(credentialProviderOptions)
296+
providerOptions.credentialProvider = async () => {
297+
try {
298+
return await rawProvider()
299+
} catch (e) {
300+
if (e instanceof Error && e.name === "CredentialsProviderError") {
301+
await new Promise<void>((resolve, reject) => {
302+
const args = ["sso", "login", ...(profile ? ["--profile", profile] : [])]
303+
const child = spawn("aws", args, { stdio: "pipe" })
304+
child.on("exit", (code) => (code === 0 ? resolve() : reject(e)))
305+
child.on("error", () => reject(e))
306+
})
307+
return await rawProvider()
308+
}
309+
throw e
310+
}
311+
}
295312
}
296313

297314
// Add custom endpoint if specified (endpoint takes precedence over baseURL)

0 commit comments

Comments
 (0)