All files / src/components/settings SapAiCoreModelPicker.tsx

88.23% Statements 105/119
100% Branches 25/25
66.66% Functions 2/3
88.23% Lines 105/119

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 1791x   1x 1x 1x   1x                                 1x 34x 34x 34x 34x 34x 34x 34x 34x 34x 33x 5x 5x   28x   33x 22x 22x 17x 17x 33x 3x 3x 3x 34x   34x                                     34x 33x   33x 33x 33x 39x     33x 33x 33x     33x 38x 38x 38x 38x   33x 94x 94x 94x 94x   33x 34x   34x 33x   33x 33x 33x 33x       33x 1x 1x 1x 4x 4x 4x     4x 33x 32x 32x 32x 27x 27x 27x         27x 37x 37x 37x     37x 27x   32x 32x 31x 31x 31x         31x 91x 91x 91x     91x 31x 32x   33x 33x   34x 34x 34x 34x   34x 34x 34x 34x 34x 34x 34x       34x   1x  
import { sapAiCoreModels } from "@shared/api"
import { SapAiCoreModelDeployment } from "@shared/proto/index.cline"
import { VSCodeDropdown, VSCodeOption } from "@vscode/webview-ui-toolkit/react"
import React, { memo, useEffect, useMemo } from "react"
import { DropdownContainer } from "./common/ModelSelector"
 
export const SAP_AI_CORE_MODEL_PICKER_Z_INDEX = 1_000
 
export interface SapAiCoreModelPickerProps {
	sapAiCoreModelDeployments: SapAiCoreModelDeployment[]
	selectedModelId: string
	selectedDeploymentId?: string
	onModelChange: (modelId: string, deploymentId: string) => void
	placeholder?: string
	useOrchestrationMode?: boolean
}
 
interface CategorizedModel {
	id: string
	isDeployed: boolean
	section: "deployed" | "supported"
}
 
const SapAiCoreModelPicker: React.FC<SapAiCoreModelPickerProps> = ({
	sapAiCoreModelDeployments,
	selectedModelId,
	selectedDeploymentId,
	onModelChange,
	placeholder = "Select a model...",
	useOrchestrationMode = false,
}) => {
	// Auto-fix deployment ID mismatch or missing deployment ID when deployments change (when ai core creds changes)
	useEffect(() => {
		if (!selectedModelId) {
			return
		}
 
		const matchingDeployment = sapAiCoreModelDeployments.find((d) => d.modelName === selectedModelId)
 
		if (matchingDeployment) {
			// deployment found - update if different
			if (!selectedDeploymentId || matchingDeployment.deploymentId !== selectedDeploymentId) {
				onModelChange(selectedModelId, matchingDeployment.deploymentId)
			}
		} else if (sapAiCoreModelDeployments.length > 0 && selectedDeploymentId) {
			// deployments loaded, but none match the selected model, which means the model is not deployed
			onModelChange(selectedModelId, "")
		}
	}, [sapAiCoreModelDeployments, selectedModelId, selectedDeploymentId, onModelChange])
 
	const handleModelChange = (e: any) => {
		const newModelId = e.target.value
 
		if (!newModelId) {
			return
		}
 
		// Find the deployment that matches the selected model
		const deployment = sapAiCoreModelDeployments.find((d) => d.modelName === newModelId)
 
		if (deployment) {
			// Deployed model: use the deployment ID
			onModelChange(deployment.modelName, deployment.deploymentId)
		} else {
			// Undeployed model: use empty deployment ID
			onModelChange(newModelId, "")
		}
	}
 
	const categorizedModels = useMemo(() => {
		const allSupportedModels = Object.keys(sapAiCoreModels)
 
		// Models that are both deployed AND supported in AI-Hydro
		const deployedModelNames = sapAiCoreModelDeployments.map((d) => d.modelName)
		const deployedAndSupported = deployedModelNames.filter((deployedModel: string) =>
			allSupportedModels.includes(deployedModel),
		)
 
		// Models that are supported in AI-Hydro but NOT deployed
		const supportedButNotDeployed = allSupportedModels.filter(
			(supportedModel: string) => !deployedModelNames.includes(supportedModel),
		)
 
		const deployed: CategorizedModel[] = deployedAndSupported.map((modelName: string) => ({
			id: modelName,
			isDeployed: true,
			section: "deployed" as const,
		}))
 
		const supported: CategorizedModel[] = supportedButNotDeployed.map((id: string) => ({
			id,
			isDeployed: false,
			section: "supported" as const,
		}))
 
		return { deployed, supported }
	}, [sapAiCoreModelDeployments])
 
	const renderOptions = () => {
		const options: React.ReactNode[] = []
 
		// Add placeholder option
		options.push(
			<VSCodeOption key="placeholder" value="">
				{placeholder}
			</VSCodeOption>,
		)
 
		if (useOrchestrationMode) {
			// Orchestration mode: Show all supported models in one flat list (no separators)
			const allSupportedModels = Object.keys(sapAiCoreModels)
			allSupportedModels.forEach((modelId) => {
				options.push(
					<VSCodeOption key={modelId} value={modelId}>
						{modelId}
					</VSCodeOption>,
				)
			})
		} else {
			// Non-orchestration mode: Show sectioned layout with separators
			// Add deployed models section
			if (categorizedModels.deployed.length > 0) {
				// Add section separator (disabled option)
				options.push(
					<VSCodeOption disabled key="deployed-header" value="">
						── Deployed Models ──
					</VSCodeOption>,
				)
 
				categorizedModels.deployed.forEach((model) => {
					options.push(
						<VSCodeOption key={model.id} value={model.id}>
							{model.id}
						</VSCodeOption>,
					)
				})
			}
 
			// Add supported but not deployed models section
			if (categorizedModels.supported.length > 0) {
				// Add section separator (disabled option)
				options.push(
					<VSCodeOption disabled key="supported-header" value="">
						── Not Deployed Models ──
					</VSCodeOption>,
				)
 
				categorizedModels.supported.forEach((model) => {
					options.push(
						<VSCodeOption key={model.id} style={{ opacity: 0.6 }} value={model.id}>
							{model.id}
						</VSCodeOption>,
					)
				})
			}
		}
 
		return options
	}
 
	return (
		<DropdownContainer className="dropdown-container" zIndex={SAP_AI_CORE_MODEL_PICKER_Z_INDEX}>
			<label htmlFor="sap-ai-core-model-dropdown">
				<span className="font-medium">Model</span>
			</label>
			<VSCodeDropdown
				id="sap-ai-core-model-dropdown"
				key={`sap-ai-core-dropdown-${useOrchestrationMode}`}
				onChange={handleModelChange}
				style={{ width: "100%" }}
				value={selectedModelId}>
				{renderOptions()}
			</VSCodeDropdown>
		</DropdownContainer>
	)
}
 
export default memo(SapAiCoreModelPicker)