@@ -3,8 +3,10 @@ import {
33 type ModelCapabilities ,
44 type ModelSelection ,
55 ProviderDriverKind ,
6+ type ServerProviderAuth ,
67 type ServerProviderModel ,
78 type ServerProviderSlashCommand ,
9+ type ServerProviderState ,
810} from "@t3tools/contracts" ;
911import * as DateTime from "effect/DateTime" ;
1012import * as Effect from "effect/Effect" ;
@@ -30,11 +32,14 @@ import {
3032 buildBooleanOptionDescriptor ,
3133 buildSelectOptionDescriptor ,
3234 buildServerProvider ,
35+ AUTH_PROBE_TIMEOUT_MS ,
3336 DEFAULT_TIMEOUT_MS ,
37+ extractAuthBoolean ,
3438 isCommandMissingCause ,
3539 parseGenericCliVersion ,
3640 providerModelsFromSettings ,
3741 spawnAndCollect ,
42+ type CommandResult ,
3843 type ServerProviderDraft ,
3944} from "../providerSnapshot.ts" ;
4045import { makeClaudeEnvironment } from "../Drivers/ClaudeHome.ts" ;
@@ -444,6 +449,81 @@ function claudeAuthMetadata(input: {
444449 return undefined ;
445450}
446451
452+ function parseClaudeAuthStatusFromOutput ( result : CommandResult ) : {
453+ readonly status : Exclude < ServerProviderState , "disabled" > ;
454+ readonly auth : Pick < ServerProviderAuth , "status" > ;
455+ readonly message ?: string ;
456+ } {
457+ const lowerOutput = `${ result . stdout } \n${ result . stderr } ` . toLowerCase ( ) ;
458+
459+ if (
460+ lowerOutput . includes ( "unknown command" ) ||
461+ lowerOutput . includes ( "unrecognized command" ) ||
462+ lowerOutput . includes ( "unexpected argument" )
463+ ) {
464+ return {
465+ status : "warning" ,
466+ auth : { status : "unknown" } ,
467+ message :
468+ "Claude Agent authentication status command is unavailable in this version of Claude." ,
469+ } ;
470+ }
471+
472+ if (
473+ lowerOutput . includes ( "not logged in" ) ||
474+ lowerOutput . includes ( "login required" ) ||
475+ lowerOutput . includes ( "authentication required" ) ||
476+ lowerOutput . includes ( "run `claude login`" ) ||
477+ lowerOutput . includes ( "run claude login" )
478+ ) {
479+ return {
480+ status : "error" ,
481+ auth : { status : "unauthenticated" } ,
482+ message : "Claude is not authenticated. Run `claude auth login` and try again." ,
483+ } ;
484+ }
485+
486+ const trimmedStdout = result . stdout . trim ( ) ;
487+ let parsedAuth : boolean | undefined ;
488+ let parsedJson = false ;
489+ if ( trimmedStdout . startsWith ( "{" ) || trimmedStdout . startsWith ( "[" ) ) {
490+ try {
491+ parsedAuth = extractAuthBoolean ( JSON . parse ( trimmedStdout ) as unknown ) ;
492+ parsedJson = true ;
493+ } catch {
494+ // Fall through to the command exit status for non-JSON output.
495+ }
496+ }
497+
498+ if ( parsedAuth === true ) {
499+ return { status : "ready" , auth : { status : "authenticated" } } ;
500+ }
501+ if ( parsedAuth === false ) {
502+ return {
503+ status : "error" ,
504+ auth : { status : "unauthenticated" } ,
505+ message : "Claude is not authenticated. Run `claude auth login` and try again." ,
506+ } ;
507+ }
508+ if ( parsedJson ) {
509+ return {
510+ status : "warning" ,
511+ auth : { status : "unknown" } ,
512+ message :
513+ "Could not verify Claude authentication status from JSON output (missing auth marker)." ,
514+ } ;
515+ }
516+ if ( result . code === 0 ) {
517+ return { status : "ready" , auth : { status : "authenticated" } } ;
518+ }
519+
520+ return {
521+ status : "warning" ,
522+ auth : { status : "unknown" } ,
523+ message : "Could not verify Claude authentication status." ,
524+ } ;
525+ }
526+
447527// ── SDK capability probe ────────────────────────────────────────────
448528
449529const CAPABILITIES_PROBE_TIMEOUT_MS = 8_000 ;
@@ -741,6 +821,34 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
741821 const dedupedSlashCommands = dedupeSlashCommands ( slashCommands ) ;
742822
743823 if ( ! capabilities ) {
824+ const authProbe = yield * runClaudeCommand (
825+ claudeSettings ,
826+ [ "auth" , "status" ] ,
827+ resolvedEnvironment ,
828+ ) . pipe ( Effect . timeoutOption ( AUTH_PROBE_TIMEOUT_MS ) , Effect . result ) ;
829+
830+ if ( Result . isSuccess ( authProbe ) && Option . isSome ( authProbe . success ) ) {
831+ const parsedAuth = parseClaudeAuthStatusFromOutput ( authProbe . success . value ) ;
832+ return buildServerProvider ( {
833+ presentation : CLAUDE_PRESENTATION ,
834+ enabled : claudeSettings . enabled ,
835+ checkedAt,
836+ models,
837+ slashCommands : dedupedSlashCommands ,
838+ probe : {
839+ installed : true ,
840+ version : parsedVersion ,
841+ status : parsedAuth . status ,
842+ auth : parsedAuth . auth ,
843+ ...( parsedAuth . message
844+ ? { message : parsedAuth . message }
845+ : versionUpgradeMessage
846+ ? { message : versionUpgradeMessage }
847+ : { } ) ,
848+ } ,
849+ } ) ;
850+ }
851+
744852 return buildServerProvider ( {
745853 presentation : CLAUDE_PRESENTATION ,
746854 enabled : claudeSettings . enabled ,
@@ -752,7 +860,10 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
752860 version : parsedVersion ,
753861 status : "warning" ,
754862 auth : { status : "unknown" } ,
755- message : "Could not verify Claude authentication status from initialization result." ,
863+ message :
864+ Result . isSuccess ( authProbe ) && Option . isNone ( authProbe . success )
865+ ? "Could not verify Claude authentication status. Timed out while running command."
866+ : "Could not verify Claude authentication status." ,
756867 } ,
757868 } ) ;
758869 }
0 commit comments