2026-07-14 01:12:05 +00:00
import { z } from "zod" ;
2026-10-07 11:45:17 +08:00
import type {
OptionalProviderLoadFailures ,
OptionalProviderLoadFailure ,
} from "./OptionalObservationProviders.js" ;
2026-07-14 01:12:05 +00:00
2026-08-10 02:28:11 +08:00
import {
providerCapability ,
type ClientFeatureAvailability ,
type ProviderCapability ,
type ToolAvailability ,
type ToolAvailabilityMode ,
type ToolUnavailabilityReason ,
} from "../contracts/toolOutputSchemaPrimitives.js" ;
2026-07-26 15:02:58 +00:00
import type { ToolKind } from "../contracts/toolContractTypes.js" ;
2026-07-14 01:12:05 +00:00
import type { JsonValue } from "../domain/jsonValue.js" ;
2026-10-09 07:04:11 +08:00
import type { ProviderAvailability } from "./AnalysisProvider.js" ;
2026-10-07 12:32:34 +08:00
import { TOOL_CONTRACTS } from "../contracts/toolContracts.js" ;
2026-08-10 02:28:11 +08:00
import {
clientRequirementsFor ,
NO_CLIENT_FEATURES ,
} from "./CapabilityClientRequirements.js" ;
2026-10-11 10:10:01 -07:00
import { compareUnicodeCodePoints } from "../domain/unicodeCodePointOrder.js" ;
2026-07-14 01:12:05 +00:00
const statusSchema = z . object ({
open : z.boolean (),
kind : z.enum ([ "executable" , "database" , "archive" , "artifact" ]). optional (),
format : z.string (). optional (),
2026-08-10 02:28:11 +08:00
capabilities : z.array ( providerCapability ),
2026-07-14 01:12:05 +00:00
});
2026-08-10 02:28:11 +08:00
type ToolAvailabilityReason = "available" | ToolUnavailabilityReason ;
2026-07-14 01:12:05 +00:00
2026-08-10 02:28:11 +08:00
type ProviderDescriptor = ProviderCapability ;
2026-07-24 13:18:58 +00:00
export type AvailabilityPolicy = {
2026-10-11 20:49:00 +09:00
readonly wasmArtifactEnabled? : boolean ;
2026-10-08 15:32:05 +08:00
readonly evmInterfaceEnabled? : boolean ;
2026-07-18 00:28:42 +00:00
readonly processCaptureEnabled : boolean ;
2026-10-07 11:45:17 +08:00
readonly optionalProviderLoadFailures? : OptionalProviderLoadFailures ;
2026-10-08 14:22:08 +08:00
readonly binaryLayoutEnabled? : boolean ;
2026-10-08 16:50:31 +08:00
readonly recordedCrashEnabled? : boolean ;
2026-10-06 22:16:04 +08:00
readonly firmwareInspectionEnabled? : boolean ;
readonly firmwareExtractionEnabled? : boolean ;
2026-10-09 07:04:11 +08:00
readonly androidAnalysisAvailability? : ProviderAvailability ;
2026-10-10 17:03:27 +02:00
readonly jebAnalysisAvailability? : ProviderAvailability ;
2026-10-07 12:46:13 +08:00
readonly javascriptRecoveryEnabled? : boolean ;
2026-10-07 14:59:52 +08:00
readonly webModuleResolutionEnabled? : boolean ;
2026-07-18 00:28:42 +00:00
readonly browserObservationEnabled? : boolean ;
2026-07-24 13:18:58 +00:00
readonly browserScenarioEnabled? : boolean ;
2026-07-18 00:28:42 +00:00
readonly electronObservationEnabled? : boolean ;
2026-08-05 19:07:46 +08:00
readonly electronAutomationEnabled? : boolean ;
2026-07-24 13:18:58 +00:00
readonly v8InspectorObservationEnabled? : boolean ;
2026-07-18 00:28:42 +00:00
};
2026-07-24 13:18:58 +00:00
2026-08-10 02:28:11 +08:00
type AvailabilityFacts = {
2026-08-04 03:00:25 +08:00
readonly defaultModeAvailable? : boolean ;
2026-08-10 02:28:11 +08:00
readonly modes? : readonly ToolAvailabilityMode [];
2026-07-18 00:28:42 +00:00
};
2026-08-10 02:28:11 +08:00
type Availability = AvailabilityFacts &
(
| { readonly reason : "available" ; readonly remediation : null }
| {
readonly reason : ToolUnavailabilityReason ;
readonly remediation : string | null ;
}
);
type NavigationModeResult =
| ( Extract < ToolAvailabilityMode , { available : true }> & {
readonly reason : "available" ;
})
| ( Extract < ToolAvailabilityMode , { available : false }> & {
readonly reason : ToolUnavailabilityReason ;
});
2026-07-18 00:28:42 +00:00
type AvailabilityContext = {
readonly name : string ;
readonly kind : ToolKind ;
readonly targetOpen : boolean ;
readonly targetKind :
| "executable"
| "database"
| "archive"
| "artifact"
| undefined ;
2026-10-09 03:02:40 +00:00
readonly targetFormat : string | undefined ;
2026-07-18 00:28:42 +00:00
readonly descriptors : ReadonlyMap < string , ProviderDescriptor >;
readonly policy : AvailabilityPolicy ;
};
2026-07-14 01:12:05 +00:00
const ENHANCED_REQUIREMENTS : Readonly < Record < string , readonly string [] >> = {
2026-10-04 16:02:36 +08:00
trace_native_values : [ "analyze_function" , "resolve_native_call_targets" ],
2026-09-30 00:41:05 +08:00
trace_native_ui_action : [
"decode_interface_builder" ,
"list_names" ,
"procedure_callees" ,
],
inspect_native_dispatch_metadata : [ "list_names" ],
2026-07-14 01:12:05 +00:00
get_objc_classes : [ "list_names" ],
get_objc_protocols : [ "list_names" ],
batch_decompile : [ "procedure_pseudo_code" ],
get_call_graph : [ "procedure_callees" , "procedure_callers" ],
analyze_swift_types : [ "list_procedures" ],
find_xrefs_to_name : [ "list_names" , "xrefs" ],
binary_overview : [
"list_segments" ,
"list_documents" ,
"list_procedures" ,
"list_strings" ,
],
analyze_function : [ "analyze_function" ],
2026-07-25 14:29:28 +00:00
inspect_native_api : [ "analyze_function" ],
2026-07-14 01:12:05 +00:00
trace_feature : [
"list_strings" ,
"list_procedures" ,
"xrefs" ,
"resolve_containing_procedure" ,
],
};
2026-10-09 03:02:40 +00:00
/** Contract kinds whose operations run on the active session target. */
const ACTIVE_TARGET_KINDS : ReadonlySet < ToolKind > = new Set ([
"official-proxy" ,
"enhanced" ,
"native-provider" ,
"artifact-provider" ,
]);
/** Native contracts defined only for an active Mach-O executable. */
const MACHO_TARGET_OPERATIONS : ReadonlySet < string > = new Set ([
"inspect_macho" ,
"list_architectures" ,
"observe_native_calls" ,
"observe_native_ui" ,
"capture_native_ui_scenario" ,
]);
2026-08-04 03:00:25 +08:00
const NAVIGATION_CONTEXT_MODES = [
{
name : "current_selection" ,
requirements : [
"current_document" ,
"current_address" ,
"resolve_containing_procedure" ,
],
},
{
name : "explicit_document" ,
requirements : [ "current_address" , "resolve_containing_procedure" ],
},
] as const ;
2026-07-29 18:18:31 +00:00
2026-07-24 13:18:58 +00:00
/** Build stable per-operation availability for discovery and tool visibility. */
2026-07-14 01:12:05 +00:00
export const buildCapabilityInventory = (
sessionStatus : JsonValue ,
2026-07-18 00:28:42 +00:00
policy : AvailabilityPolicy ,
2026-07-24 13:18:58 +00:00
clientFeatures : ClientFeatureAvailability = NO_CLIENT_FEATURES ,
2026-08-10 02:28:11 +08:00
) : readonly ToolAvailability [] => {
2026-07-14 01:12:05 +00:00
const status = statusSchema . parse ( sessionStatus );
2026-07-23 07:09:16 +00:00
const descriptors = new Map < string , ProviderDescriptor >(
2026-08-10 02:28:11 +08:00
status . capabilities . map (( descriptor ) => [ descriptor . operation , descriptor ]),
2026-07-14 01:12:05 +00:00
);
2026-10-07 12:32:34 +08:00
return TOOL_CONTRACTS . map (( contract ) : ToolAvailability => {
2026-07-18 00:28:42 +00:00
const availability = availabilityFor ({
name : contract.name ,
kind : contract.kind ,
targetOpen : status.open ,
targetKind : status.kind ,
2026-10-09 03:02:40 +00:00
targetFormat : status.format ,
2026-07-14 01:12:05 +00:00
descriptors ,
policy ,
2026-07-18 00:28:42 +00:00
});
2026-07-24 13:18:58 +00:00
const clientRequirements = clientRequirementsFor (
contract . name ,
clientFeatures ,
);
const clientBlocked = clientRequirements . missing_required . length > 0 ;
2026-08-10 02:28:11 +08:00
const facts = {
2026-07-14 01:12:05 +00:00
name : contract.name ,
surface : contract.kind ,
2026-08-04 03:00:25 +08:00
...( availability . defaultModeAvailable === undefined
? {}
: { default_mode_available : availability.defaultModeAvailable }),
...( availability . modes === undefined
? {}
2026-08-10 02:28:11 +08:00
: { modes : [... availability . modes ] }),
2026-07-24 13:18:58 +00:00
client_requirements : clientRequirements ,
2026-07-16 18:49:35 +08:00
effects : { ... contract . effects },
2026-07-14 01:12:05 +00:00
annotations : {
read_only : contract.annotations.readOnlyHint ?? false ,
destructive : contract.annotations.destructiveHint ?? false ,
idempotent : contract.annotations.idempotentHint ?? false ,
open_world : contract.annotations.openWorldHint ?? true ,
},
};
2026-08-10 02:28:11 +08:00
if ( clientBlocked )
return {
... facts ,
available : false ,
reason : "client_capability_missing" ,
remediation : `Use an MCP client that supports: ${ clientRequirements . missing_required . join ( ", " ) } .` ,
};
return availability . reason === "available"
? {
... facts ,
available : true ,
reason : availability.reason ,
remediation : null ,
}
: {
... facts ,
available : false ,
reason : availability.reason ,
remediation : availability.remediation ,
};
2026-10-11 10:10:01 -07:00
}). sort (( left , right ) => compareUnicodeCodePoints ( left . name , right . name ));
2026-07-14 01:12:05 +00:00
};
2026-07-18 00:28:42 +00:00
const availabilityFor = ( context : AvailabilityContext ) : Availability => {
2026-10-07 21:45:07 +08:00
// These built-in workflows accept a caller-selected endpoint; they do not load the optional passive provider.
if (
context . name === "observe_web_execution" ||
context . name === "inspect_web_event_listeners"
)
return { reason : "available" , remediation : null };
2026-08-04 03:00:25 +08:00
if ( context . name === "get_navigation_context" )
return navigationContextAvailability ( context . descriptors );
2026-07-21 19:51:11 +08:00
const javascriptApplication = javascriptApplicationAvailability ( context );
if ( javascriptApplication !== null ) return javascriptApplication ;
2026-10-07 11:45:17 +08:00
const failure = optionalProviderFailureFor ( context );
if ( failure !== undefined )
return {
reason : "provider_unavailable" ,
remediation : `Adapter ${ failure . providerId } could not load: ${ failure . reason } . Repair the adapter installation and restart REA; other capabilities remain available.` ,
};
2026-10-05 20:16:44 +08:00
const workflowAvailability = workflowAvailabilityFor ( context );
if ( workflowAvailability !== null ) return workflowAvailability ;
2026-10-09 03:02:40 +00:00
const hostDecision = hostAvailability ( context );
if ( hostDecision !== null ) return hostDecision ;
2026-07-18 00:28:42 +00:00
const targetDecision = targetAvailability ( context );
if ( targetDecision !== null ) return targetDecision ;
return providerAvailability ( context );
};
2026-10-07 11:45:17 +08:00
const optionalProviderFailureFor = ({
name ,
kind ,
policy ,
} : AvailabilityContext ) : OptionalProviderLoadFailure | undefined => {
const failures = policy . optionalProviderLoadFailures ;
if ( name === "capture_browser_scenario" )
return failures ? . browserScenarioCapture ;
if ( kind === "browser-provider" ) return failures ? . browserObservation ;
if ( name === "capture_electron_scenario" )
return failures ? . electronActiveObservation ;
if ( kind === "electron-provider" ) return failures ? . electronObservation ;
if ( kind === "runtime-provider" )
return failures ? . javascriptRuntimeObservation ;
return undefined ;
};
2026-07-21 19:51:11 +08:00
const javascriptApplicationAvailability = ({
name ,
} : AvailabilityContext ) : Availability | null => {
if ( name === "analyze_javascript_application" )
2026-09-29 07:08:46 +08:00
return { reason : "available" , remediation : null };
2026-07-21 19:51:11 +08:00
return name === "reconcile_javascript_runtime"
? { reason : "available" , remediation : null }
: null ;
};
2026-10-05 20:16:44 +08:00
const workflowAvailabilityFor = ({
2026-07-18 00:28:42 +00:00
name ,
kind ,
policy ,
} : AvailabilityContext ) : Availability | null => {
2026-10-07 14:59:52 +08:00
if ( name === "trace_web_module_imports" )
return {
reason : "available" ,
remediation : null ,
defaultModeAvailable : policy.webModuleResolutionEnabled === true ,
modes : [
{
name : "sources-without-literal-imports" ,
available : true ,
missing_operations : [],
remediation : null ,
},
policy . webModuleResolutionEnabled === true
? {
name : "native-literal-resolution" ,
available : true ,
missing_operations : [],
remediation : null ,
}
: {
name : "native-literal-resolution" ,
available : false ,
missing_operations : [ "native-module-resolver" ],
remediation :
"Provide an absolute REA_BROWSER_EXECUTABLE supporting import.meta.resolve; no binary target is required." ,
},
],
};
2026-10-07 12:46:13 +08:00
if ( name === "recover_javascript_sources" )
return policy . javascriptRecoveryEnabled === true
? { reason : "available" , remediation : null }
: {
reason : "provider_missing" ,
remediation :
2026-10-10 23:02:15 +08:00
"On Linux x64, provide an absolute REA_WAKARU_COMMAND for Wakaru ^1.13.0 (verified with 1.14.0) and util-linux prlimit; see docs/javascript-recovery.md#install-wakaru. No binary target is required." ,
2026-10-07 12:46:13 +08:00
};
2026-10-08 14:22:08 +08:00
if ( name === "inspect_binary_layout" )
return policy . binaryLayoutEnabled === true
? { reason : "available" , remediation : null }
: {
reason : "provider_missing" ,
remediation :
"On Linux x64, set absolute REA_PWNTOOLS_PYTHON to caller-supplied Python with pwntools 4.15.0. No active binary target is required." ,
};
2026-10-11 20:49:00 +09:00
if ( name === "inspect_wasm_artifact" )
return policy . wasmArtifactEnabled === true
? { reason : "available" , remediation : null }
: {
reason : "provider_missing" ,
remediation :
"Supply WABT 1.0.42 via absolute REA_WABT_BIN_DIRECTORY. No active binary target is required; tools are checked at execution." ,
};
2026-10-11 00:02:06 +09:00
if ( name === "inspect_pe_resources" )
return { reason : "available" , remediation : null };
2026-10-08 15:32:05 +08:00
if ( name === "inspect_evm_interface" )
return policy . evmInterfaceEnabled === true
? { reason : "available" , remediation : null }
: {
reason : "provider_missing" ,
remediation :
"The bundled offline EVM interface profile is currently real-verified on Linux x64. No active binary target or chain endpoint is required." ,
};
2026-10-08 16:50:31 +08:00
if ( name === "inspect_recorded_crash" )
return policy . recordedCrashEnabled === true
? { reason : "available" , remediation : null }
: {
reason : "provider_missing" ,
remediation :
"On Linux x64, set absolute REA_PWNTOOLS_PYTHON to caller-supplied Python with pwntools 4.15.0. No active binary target is required." ,
};
2026-10-06 22:16:04 +08:00
if ( kind === "firmware-provider" ) {
const enabled =
name === "inspect_firmware_regions"
? policy.firmwareInspectionEnabled
: policy.firmwareExtractionEnabled ;
return enabled === true
? { reason : "available" , remediation : null }
: {
reason : "provider_missing" ,
remediation :
2026-10-07 12:19:28 +08:00
"On Linux, provide an absolute REA_BINWALK_COMMAND (Binwalk 3.1.x, verified with 3.1.0) or REA_UNBLOB_COMMAND (Unblob 26.6.x, verified with 26.6.4) path and util-linux prlimit. Extraction also requires the selected format’ s external extractor." ,
2026-10-06 22:16:04 +08:00
};
}
2026-10-06 20:46:48 +08:00
if ( kind === "android-provider" )
2026-10-09 07:04:11 +08:00
return policy . androidAnalysisAvailability ? . status === "available"
2026-10-06 20:46:48 +08:00
? { reason : "available" , remediation : null }
: {
2026-10-09 07:04:11 +08:00
reason :
policy.androidAnalysisAvailability?.code === "unsupported_host"
? "unsupported_host"
: "provider_missing" ,
2026-10-06 20:46:48 +08:00
remediation :
2026-10-09 07:04:11 +08:00
policy.androidAnalysisAvailability?.reason ??
2026-10-09 20:34:42 +08:00
"Set REA_JADX_MCP_JAR to a caller-supplied jadx-headless-mcp 0.7.1 JAR and provide a full JDK on Linux, macOS, or Windows x64 with its matching bundled native controls." ,
2026-10-06 20:46:48 +08:00
};
2026-10-10 17:03:27 +02:00
if ( kind === "jeb-provider" )
return policy . jebAnalysisAvailability ? . status === "available"
? { reason : "available" , remediation : null }
: {
reason : "provider_missing" ,
remediation :
policy.jebAnalysisAvailability?.reason ??
"Start a JEB client serving MCP (GUI client, or headless via its programmatic launcher) and point REA_JEB_MCP_URL at it (default http://127.0.0.1:8425/mcp). REA does not install or launch JEB." ,
};
2026-10-05 20:16:44 +08:00
const browser = browserProviderAvailability ( name , kind , policy );
2026-07-24 13:18:58 +00:00
if ( browser !== null ) return browser ;
2026-07-14 01:12:05 +00:00
if ( name === "capture_process_scenario" && ! policy . processCaptureEnabled )
return {
2026-10-05 20:16:44 +08:00
reason : "unsupported_host" ,
remediation : "Run process capture on a supported Linux or macOS host." ,
2026-07-16 14:31:37 +08:00
};
if ( kind === "application" ) return { reason : "available" , remediation : null };
2026-10-05 20:16:44 +08:00
const electron = electronProviderAvailability ( name , kind , policy );
2026-08-05 19:07:46 +08:00
if ( electron !== null ) return electron ;
2026-07-24 13:18:58 +00:00
if ( kind === "runtime-provider" )
return policy . v8InspectorObservationEnabled === true
2026-07-14 17:47:50 +08:00
? { reason : "available" , remediation : null }
: {
2026-10-05 20:16:44 +08:00
reason : "provider_missing" ,
2026-07-14 17:47:50 +08:00
remediation :
2026-10-05 20:16:44 +08:00
"Start REA with its JavaScript runtime provider available." ,
2026-07-14 17:47:50 +08:00
};
2026-07-14 01:12:05 +00:00
if ( kind === "session" ) return { reason : "available" , remediation : null };
2026-07-18 00:28:42 +00:00
return null ;
};
2026-10-05 20:16:44 +08:00
const electronProviderAvailability = (
2026-08-05 19:07:46 +08:00
name : string ,
kind : ToolKind ,
policy : AvailabilityPolicy ,
) : Availability | null => {
if ( kind !== "electron-provider" ) return null ;
if ( name === "capture_electron_scenario" )
return policy . electronAutomationEnabled === true
? { reason : "available" , remediation : null }
: {
2026-10-05 20:16:44 +08:00
reason : "provider_missing" ,
2026-08-05 19:07:46 +08:00
remediation :
2026-10-05 20:16:44 +08:00
"Start REA with the Electron scenario provider available." ,
2026-08-05 19:07:46 +08:00
};
return policy . electronObservationEnabled === true
? { reason : "available" , remediation : null }
: {
2026-10-05 20:16:44 +08:00
reason : "provider_missing" ,
2026-08-05 19:07:46 +08:00
remediation :
2026-10-05 20:16:44 +08:00
"Start REA with its Electron observation provider available." ,
2026-08-05 19:07:46 +08:00
};
};
2026-10-05 20:16:44 +08:00
const browserProviderAvailability = (
2026-07-24 13:18:58 +00:00
name : string ,
kind : ToolKind ,
policy : AvailabilityPolicy ,
) : Availability | null => {
if ( name === "capture_browser_scenario" )
return policy . browserScenarioEnabled === true
? { reason : "available" , remediation : null }
: {
2026-10-05 20:16:44 +08:00
reason : "provider_missing" ,
2026-07-24 13:18:58 +00:00
remediation :
2026-10-05 20:16:44 +08:00
"Start REA with its browser scenario provider available." ,
2026-07-24 13:18:58 +00:00
};
if ( kind !== "browser-provider" ) return null ;
return policy . browserObservationEnabled === true
? { reason : "available" , remediation : null }
: {
2026-10-05 20:16:44 +08:00
reason : "provider_missing" ,
2026-07-24 13:18:58 +00:00
remediation :
2026-10-05 20:16:44 +08:00
"Start REA with its browser observation provider available." ,
2026-07-24 13:18:58 +00:00
};
};
2026-10-09 03:02:40 +00:00
/**
* Opening another target cannot make an operation run on an unsupported host.
* Composed operations are decided by their requirements, which another
* provider may satisfy.
*/
const hostAvailability = ({
descriptors ,
kind ,
name ,
} : AvailabilityContext ) : Availability | null => {
if ( kind === "enhanced" ) return null ;
const descriptor = descriptors . get ( name );
return descriptor ? . available === false &&
descriptor . availability_code === "unsupported_host"
? { reason : "unsupported_host" , remediation : descriptor.reason }
: null ;
};
2026-07-18 00:28:42 +00:00
const targetAvailability = ({
2026-10-09 03:02:40 +00:00
name ,
2026-07-18 00:28:42 +00:00
kind ,
targetKind ,
2026-10-09 03:02:40 +00:00
targetFormat ,
2026-07-18 00:28:42 +00:00
targetOpen ,
} : AvailabilityContext ) : Availability | null => {
2026-10-09 03:02:40 +00:00
if ( ! targetOpen && ACTIVE_TARGET_KINDS . has ( kind ))
2026-07-14 01:12:05 +00:00
return {
reason : "target_required" ,
remediation : "Call open_binary with a supported local target." ,
};
if (
targetKind !== undefined &&
targetKind !== "executable" &&
targetKind !== "database" &&
( kind === "official-proxy" || kind === "enhanced" )
)
return {
reason : "target_unsupported" ,
remediation :
"Inventory or extract a native executable, then call open_binary on that executable." ,
};
2026-10-09 03:02:40 +00:00
if (
targetOpen &&
MACHO_TARGET_OPERATIONS . has ( name ) &&
( targetKind !== "executable" || targetFormat !== "mach-o" )
)
return {
reason : "target_unsupported" ,
remediation :
"Call open_binary with a Mach-O executable or an app bundle whose main executable is Mach-O." ,
};
2026-07-18 00:28:42 +00:00
return null ;
};
const providerAvailability = ({
descriptors ,
kind ,
name ,
} : AvailabilityContext ) : Availability => {
2026-07-14 01:12:05 +00:00
if ( kind === "enhanced" ) return composedAvailability ( name , descriptors );
const descriptor = descriptors . get ( name );
2026-07-24 13:18:58 +00:00
if (
descriptor === undefined &&
( kind === "artifact-provider" ||
kind === "managed-provider" ||
kind === "native-provider" )
)
return { reason : "available" , remediation : null };
2026-07-14 01:12:05 +00:00
if ( descriptor === undefined )
return {
reason : "provider_missing" ,
remediation :
"Install or configure a provider that declares this operation." ,
};
return descriptor . available
? { reason : "available" , remediation : null }
: {
reason : "provider_unavailable" ,
remediation :
descriptor.reason ?? "Choose another target or configured provider." ,
};
};
const composedAvailability = (
name : string ,
2026-07-23 07:09:16 +00:00
descriptors : ReadonlyMap < string , ProviderDescriptor >,
2026-08-10 02:28:11 +08:00
) : Availability => {
2026-07-14 01:12:05 +00:00
const requirements = ENHANCED_REQUIREMENTS [ name ];
if ( requirements === undefined )
return {
reason : "provider_missing" ,
remediation : "No provider composition is declared for this operation." ,
};
2026-07-29 18:18:31 +00:00
return composedAvailabilityFor ( requirements , descriptors );
};
const composedAvailabilityFor = (
requirements : readonly string [],
descriptors : ReadonlyMap < string , ProviderDescriptor >,
) : Availability => {
2026-07-14 01:12:05 +00:00
const missing = requirements . find (( operation ) => ! descriptors . has ( operation ));
if ( missing !== undefined )
return {
reason : "provider_missing" ,
remediation : `Configure a provider for required operation ${ missing } .` ,
};
const unavailable = requirements
. map (( operation ) => descriptors . get ( operation ))
. find (( descriptor ) => descriptor ? . available === false );
if ( unavailable !== undefined )
return {
reason :
2026-07-23 07:09:16 +00:00
unavailable.availability_code === "unsupported_host"
2026-07-14 01:12:05 +00:00
? "unsupported_host"
: "provider_unavailable" ,
remediation :
unavailable.reason ?? "Choose another target or configured provider." ,
};
return { reason : "available" , remediation : null };
};
2026-08-04 03:00:25 +08:00
const navigationContextAvailability = (
descriptors : ReadonlyMap < string , ProviderDescriptor >,
) : Availability => {
const modeResults : NavigationModeResult [] = NAVIGATION_CONTEXT_MODES . map (
( mode ) => {
const availability = composedAvailabilityFor (
mode . requirements ,
descriptors ,
);
2026-08-10 02:28:11 +08:00
if ( availability . reason === "available" )
return {
name : mode.name ,
available : true ,
reason : availability.reason ,
missing_operations : [],
remediation : null ,
};
2026-08-04 03:00:25 +08:00
return {
name : mode.name ,
2026-08-10 02:28:11 +08:00
available : false ,
2026-08-04 03:00:25 +08:00
reason : availability.reason ,
missing_operations : mode.requirements.filter (( operation ) => {
const descriptor = descriptors . get ( operation );
return descriptor === undefined || descriptor . available === false ;
}),
remediation : availability.remediation ,
};
},
);
2026-08-10 02:28:11 +08:00
const modes : ToolAvailabilityMode [] = modeResults . map (( mode ) =>
mode . available
? {
name : mode.name ,
available : true ,
missing_operations : [],
remediation : null ,
}
: {
name : mode.name ,
available : false ,
missing_operations : [... mode . missing_operations ],
remediation : mode.remediation ,
},
2026-08-04 03:00:25 +08:00
);
const availableMode = modeResults . find (( mode ) => mode . available );
2026-08-10 02:28:11 +08:00
const failures = modeResults . flatMap (( mode ) =>
mode . available ? [] : [ mode ],
);
const failure = failures . sort (
( left , right ) =>
navigationFailurePriority ( left . reason ) -
navigationFailurePriority ( right . reason ),
)[ 0 ];
2026-08-04 03:00:25 +08:00
const specificFailureRemediation =
failure ? . reason === "provider_missing" ? undefined : failure ? . remediation ;
2026-08-10 02:28:11 +08:00
const facts = {
2026-08-04 03:00:25 +08:00
defaultModeAvailable : modes [ 0 ] ? . available ?? false ,
modes ,
};
2026-08-10 02:28:11 +08:00
if ( availableMode !== undefined )
return { ... facts , reason : "available" , remediation : null };
return {
... facts ,
reason : failure?.reason ?? "provider_missing" ,
remediation :
specificFailureRemediation ??
`Configure a provider for one of: ${ modes
. flatMap (( mode ) => mode . missing_operations )
. join ( ", " ) } .` ,
};
2026-08-04 03:00:25 +08:00
};
const navigationFailurePriority = ( reason : ToolAvailabilityReason ) : number => {
switch ( reason ) {
case "unsupported_host" :
return 0 ;
case "provider_unavailable" :
return 1 ;
case "provider_missing" :
return 2 ;
default :
return 3 ;
}
};