tryDecodeChatGptCredentials function
Decodes value into ChatGptOAuthCredentials, or null when it is not
credential-shaped at all — a plain Bearer access token (issue #1704), a
partial JSON object, or garbage. A partial JSON object that at least
names its access_token degrades to that bearer (no refresh); a plain
string is used verbatim. Callers treat null as raw-bearer use (no
proactive/reactive refresh) instead of failing the turn.
Implementation
ChatGptOAuthCredentials? tryDecodeChatGptCredentials(String value) {
try {
return ChatGptOAuthCredentials.decode(value);
} on FormatException {
final Object? decoded;
try {
decoded = jsonDecode(value);
} on Object {
return null;
}
if (decoded is Map) {
final accessToken = decoded['access_token'];
if (accessToken is String && accessToken.isNotEmpty) {
return ChatGptOAuthCredentials(
accessToken: accessToken,
refreshToken: '',
idToken: '',
);
}
}
return null;
}
}