RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
feat(bedrock): support API key authentication (#18301)
He Wang2ef9d373ee1b
diff --git a/api/apps/restful_apis/provider_api.py b/api/apps/restful_apis/provider_api.pyindex 537e290e04..e822e285a4 100644--- a/api/apps/restful_apis/provider_api.py+++ b/api/apps/restful_apis/provider_api.py@@ -638,6 +638,8 @@ async def update_provider_instance(tenant_id: str = None, provider_id_or_name: sbase_url = data["base_url"]region = data.get("region", "default")model_info = data["model_info"]+ if not isinstance(model_info, list):+ return get_error_argument_result(message="model_info must be an array")verify = data.get("verify", True)try:diff --git a/api/apps/services/provider_api_service.py b/api/apps/services/provider_api_service.pyindex 4355cb7d94..7c3daae1bb 100644--- a/api/apps/services/provider_api_service.py+++ b/api/apps/services/provider_api_service.py@@ -104,6 +104,40 @@ def _normalize_provider_api_key(provider_name: str, api_key: str | dict | None):return api_key+def _bedrock_api_key_config(api_key: str | dict | None) -> dict | None:+ if isinstance(api_key, dict):+ config = api_key+ elif isinstance(api_key, str):+ try:+ config = json.loads(api_key)+ except json.JSONDecodeError:+ return None+ else:+ return None+ if isinstance(config, dict) and config.get("auth_mode") == "bedrock_api_key":+ return config+ return None+++def _validate_bedrock_api_key_config(api_key: str | dict | None) -> dict[str, object] | None:+ config = _bedrock_api_key_config(api_key)+ if config is None:+ return None++ bedrock_api_key = config.get("bedrock_api_key")+ if not isinstance(bedrock_api_key, str) or not bedrock_api_key.strip():+ raise ValueError("Bedrock API key must be provided")+ bedrock_region = config.get("bedrock_region")+ if not isinstance(bedrock_region, str) or not bedrock_region.strip():+ raise ValueError("AWS region must be provided")++ return {+ **config,+ "bedrock_api_key": bedrock_api_key.strip(),+ "bedrock_region": bedrock_region.strip(),+ }++def _factory_llm_name(llm: dict) -> str:return llm.get("name") or llm.get("llm_name", "")@@ -228,7 +262,11 @@ def show_provider(provider_id_or_name: str):return True, {"base_url": {"default": factory_info.get("url", "")}, "name": factory_info["name"], "total_models": len(factory_info.get("llm", []))}-async def list_provider_models(provider_id_or_name: str, api_key: str = None, base_url: str = None):+async def list_provider_models(+ provider_id_or_name: str,+ api_key: str | dict | None = None,+ base_url: str | None = None,+):"""List all models for a provider from the LLM dictionary.@@ -257,16 +295,36 @@ async def list_provider_models(provider_id_or_name: str, api_key: str = None, bamodel_base_url = _normalize_provider_base_url(provider_name, base_url) or factory_info[0].get("url", "")remote_models = []- if provider_name in ModelMeta:- remote_models = await ModelMeta[provider_name](api_key, model_base_url).get_model_list()+ bedrock_api_key_config = _bedrock_api_key_config(api_key)+ should_fetch_remote = provider_name in ModelMeta and (provider_name != "Bedrock" or bedrock_api_key_config is not None)+ if should_fetch_remote:+ try:+ remote_models = await ModelMeta[provider_name](api_key, model_base_url).get_model_list()+ except ValueError as error:+ if provider_name == "Bedrock":+ return False, str(error)+ raise++ if provider_name == "Bedrock" and bedrock_api_key_config is not None and not remote_models:+ return False, "No Bedrock models were discovered"if not static_llms and not remote_models:return True, []- # Merge static and remote models, preferring remote_models on name conflicts- merged = {m["name"]: m for m in static_llms}- merged.update({m["name"]: m for m in remote_models})- models = list(merged.values())+ if provider_name == "Bedrock" and bedrock_api_key_config is not None:+ static_models = {model["name"]: model for model in static_llms}+ models = [+ {+ **model,+ "max_tokens": static_models.get(model["name"], {}).get("max_tokens", model.get("max_tokens", 8192)),+ }+ for model in remote_models+ ]+ else:+ # Merge static and remote models, preferring remote_models on name conflicts+ merged = {m["name"]: m for m in static_llms}+ merged.update({m["name"]: m for m in remote_models})+ models = list(merged.values())models.sort(key=lambda x: x["name"])return True, models@@ -305,7 +363,15 @@ def show_provider_model(provider_id_or_name: str, model_name: str):async def update_provider_instance(- tenant_id: str, provider_id_or_name: str, instance_id_or_name: str, instance_name: str, api_key: str | dict, base_url: str, region: str, model_info: list[dict] = None, verify: bool = True+ tenant_id: str,+ provider_id_or_name: str,+ instance_id_or_name: str,+ instance_name: str,+ api_key: str | dict,+ base_url: str,+ region: str,+ model_info: list[dict] = None,+ verify: bool = True,):"""Update a provider instance.@@ -355,6 +421,15 @@ async def update_provider_instance(base_url = _normalize_provider_base_url(provider_name, base_url)api_key = _normalize_provider_api_key(provider_name, api_key)+ region = (region or "").strip()++ try:+ bedrock_api_key_config = _validate_bedrock_api_key_config(api_key) if provider_name == "Bedrock" else None+ except ValueError as error:+ return False, str(error)+ bedrock_api_key_auth = bedrock_api_key_config is not None+ if bedrock_api_key_auth:+ api_key = bedrock_api_key_configapi_key_str = ""if api_key:@@ -362,7 +437,8 @@ async def update_provider_instance(# Verify api_keymodel_verify_result = {}- if verify:+ runtime_verify = verify and not bedrock_api_key_auth+ if runtime_verify:success, msg, model_verify_result = await verify_api_key(provider_name, api_key, base_url, region, model_info)if not success:return False, msg@@ -409,7 +485,7 @@ async def update_provider_instance(model_name = model.get("model_name")if not model_name:continue- if verify:+ if runtime_verify:verify_status = model_verify_result.get(model_name, ModelVerifyStatusEnum.UNKNOWN.value)if model.get("extra"):model["extra"].update({"verify": verify_status})@@ -424,7 +500,7 @@ async def update_provider_instance(if target_model_type != existing_model_names[model_name].model_type:update_dict["model_type"] = target_model_typemerged_extra = json.loads(existing_model_names[model_name].extra) if existing_model_names[model_name].extra else {}- merged_extra.update(model["extra"])+ merged_extra.update(model.get("extra") or {})if "max_tokens" in model:merged_extra.update({"max_tokens": model["max_tokens"]})update_dict["extra"] = json.dumps(merged_extra)@@ -454,7 +530,7 @@ async def update_provider_instance("is_tools": llm.get("is_tools", False),"thinking": "thinking" in llm.get("features", []),}- if verify:+ if runtime_verify:verify_status = model_verify_result.get(llm_name, ModelVerifyStatusEnum.UNKNOWN.value)db_extra_fields["verify"] = verify_statusdb_extra.update(db_extra_fields)@@ -466,7 +542,7 @@ async def update_provider_instance("is_tools": llm.get("is_tools", False),"thinking": "thinking" in llm.get("features", []),}- if verify:+ if runtime_verify:verify_status = model_verify_result.get(llm_name, ModelVerifyStatusEnum.UNKNOWN.value)extra_fields["verify"] = verify_statussuccess, _msg = add_model_to_instance(@@ -474,7 +550,8 @@ async def update_provider_instance()if not success:msg += _msg-+ if msg:+ return False, msgreturn True, "success"@@ -515,6 +592,7 @@ async def create_provider_instance(tenant_id: str, provider_id_or_name: str, insbase_url = _normalize_provider_base_url(provider_name, base_url)api_key = _normalize_provider_api_key(provider_name, api_key)+ region = (region or "").strip()if instance_name == "default":return False, "Instance name cannot be 'default'"@@ -524,13 +602,26 @@ async def create_provider_instance(tenant_id: str, provider_id_or_name: str, insif provider_name not in allowed_factories:return False, f"Provider '{provider_name}' is not allowed"+ try:+ bedrock_api_key_config = _validate_bedrock_api_key_config(api_key) if provider_name == "Bedrock" else None+ except ValueError as error:+ return False, str(error)+ bedrock_api_key_auth = bedrock_api_key_config is not None+ if bedrock_api_key_auth:+ api_key = bedrock_api_key_config+api_key_str = ""if api_key:api_key_str = api_key if isinstance(api_key, str) else json.dumps(api_key)- success, verify_msg, model_verify_result = await verify_api_key(provider_name, api_key, base_url, region, model_info)- if not success:- return False, verify_msg+ if bedrock_api_key_auth:+ if not model_info:+ return False, "At least one Bedrock model must be selected"+ model_verify_result = {}+ else:+ success, verify_msg, model_verify_result = await verify_api_key(provider_name, api_key, base_url, region, model_info)+ if not success:+ return False, verify_msgextra_fields = {}if base_url:diff --git a/docs/guides/models/llm_api_key_setup.md b/docs/guides/models/llm_api_key_setup.mdindex 2aa6729ba4..91c82dbdc9 100644--- a/docs/guides/models/llm_api_key_setup.md+++ b/docs/guides/models/llm_api_key_setup.md@@ -49,6 +49,14 @@ To configure a model provider:Do not expose your API Key. An incorrect Base URL causes connection verification or model calls to fail. When using a compatible API, confirm whether the path must include `/v1`.:::+#### Amazon Bedrock API keys++For **Bedrock**, select **API Key**, enter the Bedrock API key and AWS Region, then list and select the models available to that key. RAGFlow keeps the key scoped to that provider instance and sends it as a Bearer token only for that instance's requests.++Bedrock API key authentication does not support rerank models.++Use short-term Bedrock API keys for production whenever possible. Long-term keys remain valid until they expire or are deleted, so store them as secrets, restrict access to the RAGFlow instance, and rotate them regularly.+### Verify the ConnectionAfter filling in **API Key** and **Base URL**, verify the connection first. If verification fails, check the API Key, Base URL, network connection, account quota, and model availability.diff --git a/internal/entity/models/bedrock.go b/internal/entity/models/bedrock.goindex bd3ba5e57b..0f4dcf99cf 100644--- a/internal/entity/models/bedrock.go+++ b/internal/entity/models/bedrock.go@@ -26,6 +26,8 @@ import ("fmt""io""net/http"+ "regexp"+ "slices""strings""time"@@ -63,15 +65,26 @@ const (bedrockControlHostTmpl = "bedrock.%s.amazonaws.com")-// Bedrock authentication modes mirroring the Python LiteLLMBase-// dispatch at rag/llm/chat_model.py:1872. The API key is stored as a+// Bedrock authentication modes mirror the Python LiteLLMBase+// dispatch in rag/llm/chat_model.py. The API key is stored as a// JSON blob containing one of these modes plus its required fields.const (bedrockAuthAccessKey = "access_key_secret"bedrockAuthIAMRole = "iam_role"bedrockAuthAssumeRole = "assume_role"+ bedrockAuthAPIKey = "bedrock_api_key")+var bedrockRegionPattern = regexp.MustCompile(`^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$`)++// ValidateBedrockRegion reports whether region can be used in an AWS endpoint.+func ValidateBedrockRegion(region string) error {+ if !bedrockRegionPattern.MatchString(region) {+ return fmt.Errorf("bedrock: invalid region %q", region)+ }+ return nil+}+// bedrockAssumeRoleSession identifies temporary sessions in CloudTrail// when iam_role mode triggers STS AssumeRole. Matches the Python// implementation's RoleSessionName so audit logs stay consistent.@@ -79,22 +92,21 @@ const bedrockAssumeRoleSession = "BedrockSession"// BedrockModel implements ModelDriver for AWS Bedrock.//-// Bedrock is AWS-signed (SigV4) rather than OpenAI-compatible, so this-// driver differs from the SaaS cluster in three ways:-// - Authentication uses AWS SigV4 over an access key + secret (and-// optionally a session token), not a static Bearer token.+// Bedrock is not OpenAI-compatible, so this driver differs from the+// SaaS cluster in three ways:+// - Authentication uses either AWS SigV4 credentials or a Bedrock+// API key sent as a request-scoped Bearer token.// - The "api key" is a JSON blob carrying auth_mode, region, and the// mode-specific credential material (access_key_secret /-// iam_role / assume_role). This mirrors the Python implementation-// at rag/llm/chat_model.py:1872.+// iam_role / assume_role / bedrock_api_key). This mirrors the+// Python implementation at rag/llm/chat_model.py.// - The streaming response uses the AWS event-stream binary framing// (vnd.amazon.eventstream), not Server-Sent Events. Each frame is// decoded with the aws-sdk-go-v2 eventstream package.//-// The base URL is computed from the configured region rather than-// supplied from conf/models/bedrock.json, because every Bedrock region-// has its own endpoint and the URL is fully determined by the region-// in the API key.+// The default base URL is computed from the configured region. SigV4+// modes may use a configured endpoint override; API-key mode is kept+// on the regional AWS endpoint so a tenant URL cannot receive the key.type BedrockModel struct {baseModel BaseModel}@@ -135,6 +147,7 @@ type bedrockKey struct {AWSRoleARN string `json:"aws_role_arn"`ExternalID string `json:"aws_external_id"`SessionName string `json:"role_session_name"`+ APIKey string `json:"bedrock_api_key"`}// parseBedrockKey decodes the JSON blob stored in APIConfig.ApiKey@@ -165,6 +178,11 @@ func parseBedrockKey(raw string) (*bedrockKey, error) {case bedrockAuthAssumeRole:// Default credential chain handles its own validation when// we ask config.LoadDefaultConfig to materialize credentials.+ case bedrockAuthAPIKey:+ key.APIKey = strings.TrimSpace(key.APIKey)+ if key.APIKey == "" {+ return nil, fmt.Errorf("bedrock: bedrock_api_key mode requires bedrock_api_key")+ }default:return nil, fmt.Errorf("bedrock: unsupported auth_mode %q", key.AuthMode)}@@ -176,19 +194,23 @@ func parseBedrockKey(raw string) (*bedrockKey, error) {// the same instance to a different region), falling back to the// region declared inside the API key JSON.func resolveBedrockRegion(apiConfig *APIConfig, key *bedrockKey) (string, error) {- if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" {- return *apiConfig.Region, nil+ region := key.Region+ if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" && *apiConfig.Region != "default" {+ region = *apiConfig.Region}- if key.Region == "" {+ if region == "" {return "", fmt.Errorf("bedrock: region is required (set apiConfig.Region or bedrock_region in the API key)")}- return key.Region, nil+ if err := ValidateBedrockRegion(region); err != nil {+ return "", err+ }+ return region, nil}// resolveBedrockCredentials returns AWS credentials for the chosen// auth mode. For static keys this is a one-liner; for iam_role we// load the default credential chain and ask STS to assume the role,-// matching boto3 sts_client.assume_role at rag/llm/chat_model.py:1893.+// matching boto3 sts_client.assume_role in rag/llm/chat_model.py.// For assume_role we simply expose the default chain (IRSA, instance// profile, etc.) — same semantics as the Python "default credential// chain" comment.@@ -302,9 +324,8 @@ func (b *BedrockModel) embeddingSuffix() string {// bedrockRuntimeURL builds the per-region runtime endpoint URL for a// given Bedrock operation. Bedrock paths are deployment-style:-// {host}/model/{modelId}/{op}. Any user-supplied override in BaseURL-// wins so on-premises proxies (e.g. CloudFront-fronted VPC endpoints)-// keep working.+// {host}/model/{modelId}/{op}. SigV4 modes may use a BaseURL override;+// API-key mode rejects overrides before sending the credential.func (b *BedrockModel) bedrockRuntimeURL(region, modelID, op string) string {if override, ok := b.baseModel.BaseURL[region]; ok && override != "" {return joinBedrockPath(override, "model", modelID, op)@@ -562,6 +583,25 @@ func signBedrockRequest(ctx context.Context, req *http.Request, body []byte, crereturn nil}+func authorizeBedrockRequest(ctx context.Context, req *http.Request, body []byte, key *bedrockKey, service, region string) error {+ if key.AuthMode == bedrockAuthAPIKey {+ expectedHost := fmt.Sprintf(bedrockRuntimeHostTmpl, region)+ if service == bedrockControlService {+ expectedHost = fmt.Sprintf(bedrockControlHostTmpl, region)+ }+ if req.URL.Scheme != "https" || !strings.EqualFold(req.URL.Host, expectedHost) || req.URL.User != nil {+ return fmt.Errorf("bedrock: API key authentication requires the regional AWS Bedrock endpoint")+ }+ req.Header.Set("Authorization", "Bearer "+key.APIKey)+ return nil+ }+ creds, err := resolveBedrockCredentials(ctx, key, region)+ if err != nil {+ return err+ }+ return signBedrockRequest(ctx, req, body, creds, service, region)+}+// ChatWithMessages sends a non-streaming Converse request and returns// the joined assistant answer. ReasonContent is always non-nil per the// driver contract; Bedrock surfaces no reasoning channel today, so it@@ -595,11 +635,6 @@ func (b *BedrockModel) ChatWithMessages(ctx context.Context, modelName string, mctx, cancel := context.WithTimeout(ctx, nonStreamCallTimeout)defer cancel()- creds, err := resolveBedrockCredentials(ctx, key, region)- if err != nil {- return nil, err- }-url := b.bedrockRuntimeURL(region, modelName, b.chatSuffix())req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(raw))if err != nil {@@ -607,14 +642,13 @@ func (b *BedrockModel) ChatWithMessages(ctx context.Context, modelName string, m}req.Header.Set("Content-Type", "application/json")req.Header.Set("Accept", "application/json")- if err := signBedrockRequest(ctx, req, raw, creds, bedrockRuntimeService, region); err != nil {+ if err := authorizeBedrockRequest(ctx, req, raw, key, bedrockRuntimeService, region); err != nil {return nil, err}// codeql[go/request-forgery] False positive: AWS Bedrock endpoint is- // derived from the AWS region (operator config, see AWSConfig above),- // not from user input. The signed request enforces the destination- // via sigv4 — a tampered URL would fail signature verification.+ // derived from the validated AWS region or an operator-provided+ // endpoint override, not an arbitrary request URL.resp, err := b.baseModel.httpClient.Do(req)if err != nil {return nil, fmt.Errorf("bedrock: send request: %w", err)@@ -626,7 +660,7 @@ func (b *BedrockModel) ChatWithMessages(ctx context.Context, modelName string, mreturn nil, fmt.Errorf("bedrock: read response: %w", err)}if resp.StatusCode != http.StatusOK {- return nil, fmt.Errorf("bedrock: API request failed with status %d: %s", resp.StatusCode, string(respBody))+ return nil, fmt.Errorf("bedrock: API request failed in region %q with status %d: %s", region, resp.StatusCode, string(respBody))}var parsed bedrockConverseResponse@@ -689,11 +723,6 @@ func (b *BedrockModel) ChatStreamlyWithSender(ctx context.Context, modelName str// Background context: event streams are long-lived so we attach// no overall deadline. ResponseHeaderTimeout (set in the// constructor) still caps connection setup.- creds, err := resolveBedrockCredentials(ctx, key, region)- if err != nil {- return err- }-url := b.bedrockRuntimeURL(region, modelName, b.streamSuffix())req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(raw))if err != nil {@@ -701,14 +730,13 @@ func (b *BedrockModel) ChatStreamlyWithSender(ctx context.Context, modelName str}req.Header.Set("Content-Type", "application/json")req.Header.Set("Accept", "application/vnd.amazon.eventstream")- if err := signBedrockRequest(ctx, req, raw, creds, bedrockRuntimeService, region); err != nil {+ if err := authorizeBedrockRequest(ctx, req, raw, key, bedrockRuntimeService, region); err != nil {return err}// codeql[go/request-forgery] False positive: AWS Bedrock endpoint is- // derived from the AWS region (operator config, see AWSConfig above),- // not from user input. The signed request enforces the destination- // via sigv4 — a tampered URL would fail signature verification.+ // derived from the validated AWS region or an operator-provided+ // endpoint override, not an arbitrary request URL.resp, err := b.baseModel.httpClient.Do(req)if err != nil {return fmt.Errorf("bedrock: send request: %w", err)@@ -717,7 +745,7 @@ func (b *BedrockModel) ChatStreamlyWithSender(ctx context.Context, modelName strif resp.StatusCode != http.StatusOK {errBody, _ := io.ReadAll(resp.Body)- return fmt.Errorf("bedrock: API request failed with status %d: %s", resp.StatusCode, string(errBody))+ return fmt.Errorf("bedrock: API request failed in region %q with status %d: %s", region, resp.StatusCode, string(errBody))}// Bedrock sends final token usage inside a "metadata" event frame@@ -852,13 +880,17 @@ func extractBedrockDeltaText(payload []byte) (string, error) {return env.Delta.Text, nil}-// bedrockListModelsResponse mirrors the relevant subset of the-// control-plane response shape. The full record carries provider,-// modality, lifecycle status, etc., which we deliberately drop to-// match the ModelDriver.ListModels return type ([]string).+// bedrockListModelsResponse mirrors the control-plane fields needed to keep+// unsupported or unavailable models out of the RAGFlow catalog.type bedrockListModelsResponse struct {ModelSummaries []struct {- ModelID string `json:"modelId"`+ ModelID string `json:"modelId"`+ InputModalities []string `json:"inputModalities"`+ OutputModalities []string `json:"outputModalities"`+ InferenceTypesSupported []string `json:"inferenceTypesSupported"`+ ModelLifecycle struct {+ Status string `json:"status"`+ } `json:"modelLifecycle"`} `json:"modelSummaries"`}@@ -883,25 +915,19 @@ func (b *BedrockModel) ListModels(ctx context.Context, apiConfig *APIConfig) ([]ctx, cancel := context.WithTimeout(ctx, nonStreamCallTimeout)defer cancel()- creds, err := resolveBedrockCredentials(ctx, key, region)- if err != nil {- return nil, err- }-url := b.bedrockControlURL(region, b.modelsSuffix())req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)if err != nil {return nil, fmt.Errorf("bedrock: build request: %w", err)}req.Header.Set("Accept", "application/json")- if err = signBedrockRequest(ctx, req, nil, creds, bedrockControlService, region); err != nil {+ if err = authorizeBedrockRequest(ctx, req, nil, key, bedrockControlService, region); err != nil {return nil, err}- // derived from the AWS region (operator config, see AWSConfig above),- // not from user input. The signed request enforces the destination- // via sigv4 — a tampered URL would fail signature verification.// codeql[go/request-forgery] False positive: AWS Bedrock endpoint is+ // derived from the validated AWS region or an operator-provided+ // endpoint override, not an arbitrary request URL.resp, err := b.baseModel.httpClient.Do(req)if err != nil {return nil, fmt.Errorf("bedrock: send request: %w", err)@@ -913,7 +939,7 @@ func (b *BedrockModel) ListModels(ctx context.Context, apiConfig *APIConfig) ([]return nil, fmt.Errorf("bedrock: read response: %w", err)}if resp.StatusCode != http.StatusOK {- return nil, fmt.Errorf("bedrock: ListModels failed with status %d: %s", resp.StatusCode, string(respBody))+ return nil, fmt.Errorf("bedrock: ListModels failed in region %q with status %d: %s", region, resp.StatusCode, string(respBody))}var parsed bedrockListModelsResponse@@ -922,11 +948,27 @@ func (b *BedrockModel) ListModels(ctx context.Context, apiConfig *APIConfig) ([]}models := make([]ListModelResponse, 0, len(parsed.ModelSummaries))for _, m := range parsed.ModelSummaries {- if m.ModelID == "" {+ if m.ModelID == "" || !slices.Contains(m.InputModalities, "TEXT") || strings.Contains(strings.ToLower(m.ModelID), "rerank") {+ continue+ }+ if len(m.InferenceTypesSupported) > 0 && !slices.Contains(m.InferenceTypesSupported, "ON_DEMAND") {+ continue+ }+ if m.ModelLifecycle.Status != "" && m.ModelLifecycle.Status != "ACTIVE" {+ continue+ }++ var modelTypes []string+ if slices.Contains(m.OutputModalities, "EMBEDDING") && (strings.HasPrefix(m.ModelID, "amazon.titan-embed-text") || strings.HasPrefix(m.ModelID, "cohere.embed-")) {+ modelTypes = []string{"embedding"}+ } else if slices.Contains(m.OutputModalities, "TEXT") {+ modelTypes = []string{"chat"}+ } else {continue}models = append(models, ListModelResponse{- Name: m.ModelID,+ Name: m.ModelID,+ ModelTypes: modelTypes,})}return models, nil@@ -987,21 +1029,16 @@ func (b *BedrockModel) Embed(ctx context.Context, modelName *string, request Embctx, cancel := context.WithTimeout(ctx, nonStreamCallTimeout)defer cancel()- creds, err := resolveBedrockCredentials(ctx, key, region)- if err != nil {- return nil, err- }-if strings.HasPrefix(modelID, "amazon.titan-embed-text-") {- return b.embedTitan(ctx, modelID, request.Texts, region, creds, embeddingConfig, modelUsage)+ return b.embedTitan(ctx, modelID, request.Texts, region, key, embeddingConfig, modelUsage)}if strings.HasPrefix(modelID, "cohere.embed-") {- return b.embedCohere(ctx, modelID, request.Texts, region, creds, embeddingConfig, modelUsage)+ return b.embedCohere(ctx, modelID, request.Texts, region, key, embeddingConfig, modelUsage)}return nil, fmt.Errorf("bedrock: unsupported embedding model %q", modelID)}-func (b *BedrockModel) invokeEmbeddingModel(ctx context.Context, modelID string, body interface{}, region string, creds awssdk.Credentials) ([]byte, error) {+func (b *BedrockModel) invokeEmbeddingModel(ctx context.Context, modelID string, body interface{}, region string, key *bedrockKey) ([]byte, error) {raw, err := json.Marshal(body)if err != nil {return nil, fmt.Errorf("bedrock: marshal embedding request: %w", err)@@ -1013,14 +1050,13 @@ func (b *BedrockModel) invokeEmbeddingModel(ctx context.Context, modelID string,}req.Header.Set("Content-Type", "application/json")req.Header.Set("Accept", "application/json")- if err := signBedrockRequest(ctx, req, raw, creds, bedrockRuntimeService, region); err != nil {+ if err := authorizeBedrockRequest(ctx, req, raw, key, bedrockRuntimeService, region); err != nil {return nil, err}// codeql[go/request-forgery] False positive: AWS Bedrock endpoint is- // derived from the AWS region (operator config, see AWSConfig above),- // not from user input. The signed request enforces the destination- // via sigv4 — a tampered URL would fail signature verification.+ // derived from the validated AWS region or an operator-provided+ // endpoint override, not an arbitrary request URL.resp, err := b.baseModel.httpClient.Do(req)if err != nil {return nil, fmt.Errorf("bedrock: send embedding request: %w", err)@@ -1032,12 +1068,12 @@ func (b *BedrockModel) invokeEmbeddingModel(ctx context.Context, modelID string,return nil, fmt.Errorf("bedrock: read embedding response: %w", err)}if resp.StatusCode != http.StatusOK {- return nil, fmt.Errorf("bedrock: embedding request failed with status %d: %s", resp.StatusCode, string(respBody))+ return nil, fmt.Errorf("bedrock: embedding request failed in region %q with status %d: %s", region, resp.StatusCode, string(respBody))}return respBody, nil}-func (b *BedrockModel) embedTitan(ctx context.Context, modelID string, texts []string, region string, creds awssdk.Credentials, embeddingConfig *EmbeddingConfig, modelUsage *common.ModelUsage) ([]EmbeddingData, error) {+func (b *BedrockModel) embedTitan(ctx context.Context, modelID string, texts []string, region string, key *bedrockKey, embeddingConfig *EmbeddingConfig, modelUsage *common.ModelUsage) ([]EmbeddingData, error) {embeddings := make([]EmbeddingData, 0, len(texts))for i, text := range texts {req := bedrockTitanEmbeddingRequest{@@ -1046,7 +1082,7 @@ func (b *BedrockModel) embedTitan(ctx context.Context, modelID string, texts []sif embeddingConfig != nil && embeddingConfig.Dimension > 0 && strings.HasPrefix(modelID, "amazon.titan-embed-text-v2") {req.Dimensions = &embeddingConfig.Dimension}- respBody, err := b.invokeEmbeddingModel(ctx, modelID, req, region, creds)+ respBody, err := b.invokeEmbeddingModel(ctx, modelID, req, region, key)if err != nil {return nil, err}@@ -1065,7 +1101,7 @@ func (b *BedrockModel) embedTitan(ctx context.Context, modelID string, texts []sreturn embeddings, nil}-func (b *BedrockModel) embedCohere(ctx context.Context, modelID string, texts []string, region string, creds awssdk.Credentials, embeddingConfig *EmbeddingConfig, modelUsage *common.ModelUsage) ([]EmbeddingData, error) {+func (b *BedrockModel) embedCohere(ctx context.Context, modelID string, texts []string, region string, key *bedrockKey, embeddingConfig *EmbeddingConfig, modelUsage *common.ModelUsage) ([]EmbeddingData, error) {req := bedrockCohereEmbeddingRequest{Texts: texts,InputType: "search_document",@@ -1073,7 +1109,7 @@ func (b *BedrockModel) embedCohere(ctx context.Context, modelID string, texts []if embeddingConfig != nil && embeddingConfig.Dimension > 0 && strings.HasPrefix(modelID, "cohere.embed-v4") {req.OutputDimension = &embeddingConfig.Dimension}- respBody, err := b.invokeEmbeddingModel(ctx, modelID, req, region, creds)+ respBody, err := b.invokeEmbeddingModel(ctx, modelID, req, region, key)if err != nil {return nil, err}diff --git a/internal/entity/models/bedrock_test.go b/internal/entity/models/bedrock_test.goindex d55d3d0df9..fd26f72a07 100644--- a/internal/entity/models/bedrock_test.go+++ b/internal/entity/models/bedrock_test.go@@ -37,6 +37,10 @@ func validBedrockKey() string {return `{"auth_mode":"access_key_secret","bedrock_region":"us-east-1","bedrock_ak":"AKIATEST","bedrock_sk":"secret-test"}`}+func validBedrockAPIKey() string {+ return `{"auth_mode":"bedrock_api_key","bedrock_region":"us-east-1","bedrock_api_key":"bedrock-test-key"}`+}+// newBedrockForTest constructs a BedrockModel whose runtime and// control endpoints are both overridden to point at the supplied// httptest base URL. The override map keys ("us-east-1" and@@ -105,6 +109,12 @@ func TestParseBedrockKeyIAMRoleRequiresARN(t *testing.T) {}}+func TestParseBedrockAPIKeyRequiresValue(t *testing.T) {+ if _, err := parseBedrockKey(`{"auth_mode":"bedrock_api_key","bedrock_region":"us-east-1"}`); err == nil || !strings.Contains(err.Error(), "bedrock_api_key") {+ t.Errorf("bedrock_api_key without key: want explicit error, got %v", err)+ }+}+func TestParseBedrockKeyAssumeRoleAcceptsBareConfig(t *testing.T) {// assume_role intentionally delegates to the default credential// chain, so parseBedrockKey must accept a blob with no AK/SK/ARN.@@ -128,6 +138,11 @@ func TestResolveBedrockRegionFallsBackToKey(t *testing.T) {if err != nil || got != "us-east-1" {t.Errorf("got region=%q err=%v, want us-east-1", got, err)}+ defaultRegion := "default"+ got, err = resolveBedrockRegion(&APIConfig{Region: &defaultRegion}, key)+ if err != nil || got != "us-east-1" {+ t.Errorf("default placeholder: got region=%q err=%v, want us-east-1", got, err)+ }}func TestResolveBedrockRegionRequiresOne(t *testing.T) {@@ -370,6 +385,37 @@ func TestBedrockChatHappyPath(t *testing.T) {}}+func TestAuthorizeBedrockRequestAPIKey(t *testing.T) {+ key, err := parseBedrockKey(validBedrockAPIKey())+ if err != nil {+ t.Fatalf("parse key: %v", err)+ }+ for _, tc := range []struct {+ name string+ method string+ url string+ service string+ }{+ {name: "runtime", method: http.MethodPost, url: "https://bedrock-runtime.us-east-1.amazonaws.com/model/test/converse", service: bedrockRuntimeService},+ {name: "control", method: http.MethodGet, url: "https://bedrock.us-east-1.amazonaws.com/foundation-models", service: bedrockControlService},+ } {+ t.Run(tc.name, func(t *testing.T) {+ req, _ := http.NewRequest(tc.method, tc.url, nil)+ if err = authorizeBedrockRequest(t.Context(), req, nil, key, tc.service, "us-east-1"); err != nil {+ t.Fatalf("authorize request: %v", err)+ }+ if got := req.Header.Get("Authorization"); got != "Bearer bedrock-test-key" {+ t.Errorf("Authorization=%q, want request-scoped Bearer token", got)+ }+ })+ }++ untrusted, _ := http.NewRequest(http.MethodPost, "https://attacker.example.com/model/test/converse", nil)+ if err = authorizeBedrockRequest(t.Context(), untrusted, nil, key, bedrockRuntimeService, "us-east-1"); err == nil {+ t.Fatal("expected an untrusted endpoint to be rejected")+ }+}+func TestBedrockChatRequiresAPIKey(t *testing.T) {withSSRFBypass(t)ctx := t.Context()@@ -411,6 +457,9 @@ func TestBedrockChatPropagatesHTTPError(t *testing.T) {if err == nil || !strings.Contains(err.Error(), "401") {t.Errorf("want 401 propagated, got %v", err)}+ if err != nil && !strings.Contains(err.Error(), "us-east-1") {+ t.Errorf("want region included in error, got %v", err)+ }if err != nil && !strings.Contains(err.Error(), "InvalidSignatureException") {t.Errorf("want body included in error, got %v", err)}@@ -425,8 +474,11 @@ func TestBedrockListModelsParsesCatalog(t *testing.T) {w.Header().Set("Content-Type", "application/json")_, _ = w.Write([]byte(`{"modelSummaries": [- {"modelId":"anthropic.claude-3-haiku-20240307-v1:0"},- {"modelId":"amazon.nova-lite-v1:0"},+ {"modelId":"anthropic.claude-3-haiku-20240307-v1:0","inputModalities":["TEXT"],"outputModalities":["TEXT"],"inferenceTypesSupported":["ON_DEMAND"],"modelLifecycle":{"status":"ACTIVE"}},+ {"modelId":"amazon.nova-lite-v1:0","inputModalities":["TEXT","IMAGE"],"outputModalities":["TEXT"],"inferenceTypesSupported":["ON_DEMAND"]},+ {"modelId":"amazon.titan-embed-text-v2:0","inputModalities":["TEXT"],"outputModalities":["EMBEDDING"]},+ {"modelId":"cohere.rerank-v3-5:0","inputModalities":["TEXT"],"outputModalities":["TEXT"]},+ {"modelId":"inactive.model","inputModalities":["TEXT"],"outputModalities":["TEXT"],"modelLifecycle":{"status":"LEGACY"}},{"modelId":""}]}`))@@ -444,6 +496,7 @@ func TestBedrockListModelsParsesCatalog(t *testing.T) {want := []string{"anthropic.claude-3-haiku-20240307-v1:0","amazon.nova-lite-v1:0",+ "amazon.titan-embed-text-v2:0",}if len(got) != len(want) {t.Fatalf("len(got)=%d want %d (%v)", len(got), len(want), got)@@ -453,6 +506,15 @@ func TestBedrockListModelsParsesCatalog(t *testing.T) {t.Errorf("got[%d]=%s want %q", i, got[i].Name, want[i])}}+ if got[0].ModelTypes[0] != "chat" {+ t.Errorf("chat model types=%v", got[0].ModelTypes)+ }+ if len(got[1].ModelTypes) != 1 || got[1].ModelTypes[0] != "chat" {+ t.Errorf("image-input model types=%v", got[1].ModelTypes)+ }+ if got[2].ModelTypes[0] != "embedding" {+ t.Errorf("embedding model types=%v", got[2].ModelTypes)+ }}func TestBedrockCheckConnectionDelegates(t *testing.T) {@@ -466,8 +528,8 @@ func TestBedrockCheckConnectionDelegates(t *testing.T) {defer srv.Close()m := newBedrockForTest(srv.URL)key := validBedrockKey()- if err := m.CheckConnection(ctx, &APIConfig{ApiKey: &key}); err == nil || !strings.Contains(err.Error(), "403") {- t.Errorf("want 403 surfaced via ListModels, got %v", err)+ if err := m.CheckConnection(ctx, &APIConfig{ApiKey: &key}); err == nil || !strings.Contains(err.Error(), "403") || !strings.Contains(err.Error(), "us-east-1") {+ t.Errorf("want 403 and region surfaced via ListModels, got %v", err)}}diff --git a/internal/entity/models/model.go b/internal/entity/models/model.goindex dba48e769b..1e53aed929 100644--- a/internal/entity/models/model.go+++ b/internal/entity/models/model.go@@ -162,6 +162,7 @@ type Model struct {Name string `json:"name"`ContentLength *int `json:"content_length"`MaxOutput *int `json:"max_output"`+ MaxTokens *int `json:"max_tokens"`ModelTypes []string `json:"model_types"`Thinking *ModelThinking `json:"thinking"`Tools *ModelTools `json:"tools"`@@ -528,6 +529,9 @@ func (pm *ProviderManager) ListModels(providerName string) ([]map[string]interfa"max_batch_size": model.MaxBatchSize,"dimensions": model.Dimensions,}+ if model.MaxTokens != nil {+ modelData["max_tokens"] = *model.MaxTokens+ }if model.BatchSize != nil {modelData["batch_size"] = *model.BatchSize}diff --git a/internal/entity/models/model_test.go b/internal/entity/models/model_test.goindex 13856594c3..8e703840b7 100644--- a/internal/entity/models/model_test.go+++ b/internal/entity/models/model_test.go@@ -133,6 +133,23 @@ func TestHostedProviderConfigsLoadSharedDrivers(t *testing.T) {}}+func TestBedrockConfigPreservesEmbeddingMaxTokens(t *testing.T) {+ dir, restore := setupProviderTestDir(t, "bedrock.json")+ defer restore()++ if err := InitProviderManager(dir); err != nil {+ t.Fatalf("InitProviderManager: %v", err)+ }++ model, err := GetProviderManager().GetModelByName("Bedrock", "cohere.embed-english-v3")+ if err != nil {+ t.Fatalf("GetModelByName: %v", err)+ }+ if model.MaxTokens == nil || *model.MaxTokens != 512 {+ t.Fatalf("MaxTokens = %v, want 512", model.MaxTokens)+ }+}+func TestLocalOCRProviderConfigsLoadLocalDrivers(t *testing.T) {dir, restore := setupProviderTestDir(t, "mineru_local.json", "paddleocr_local.json")defer restore()diff --git a/internal/handler/providers.go b/internal/handler/providers.goindex e9cb00db61..a96d5674ac 100644--- a/internal/handler/providers.go+++ b/internal/handler/providers.go@@ -160,12 +160,30 @@ func (h *ProviderHandler) ListModels(c *gin.Context) {staticModels = []map[string]interface{}{}}- // 2. Attempt live API fetch when api_key and base_url are provided+ // 2. Attempt live API fetch when the provider has enough connection data.apiKey := c.Query("api_key")baseURL := c.Query("base_url")var remoteModels []map[string]interface{}+ remoteFetched := false- if apiKey != "" && baseURL != "" {+ bedrockProvider := strings.EqualFold(providerName, "Bedrock")+ bedrockAPIKeyAuth := false+ if bedrockProvider && apiKey != "" {+ var key struct {+ AuthMode string `json:"auth_mode"`+ }+ if err := json.Unmarshal([]byte(apiKey), &key); err == nil {+ bedrockAPIKeyAuth = key.AuthMode == "bedrock_api_key"+ }+ }++ canFetchRemote := apiKey != "" && baseURL != ""+ if bedrockProvider {+ // Bedrock's existing SigV4 modes keep using the static catalog. Only+ // API-key auth needs a live catalog scoped to the supplied credential.+ canFetchRemote = bedrockAPIKeyAuth+ }+ if canFetchRemote {providerInfo := dao.GetModelProviderManager().FindProvider(providerName)if providerInfo != nil && providerInfo.ModelDriver != nil {region := "default"@@ -177,17 +195,46 @@ func (h *ProviderHandler) ListModels(c *gin.Context) {Region: ®ion,}if liveModels, err := driver.ListModels(c.Request.Context(), apiConfig); err == nil {+ remoteFetched = truefor _, m := range liveModels {+ maxTokens := 8192+ if m.MaxOutput != nil {+ maxTokens = *m.MaxOutput+ }remoteModels = append(remoteModels, map[string]interface{}{"name": m.Name,"model_types": m.ModelTypes,- "max_output": m.MaxOutput,+ "max_tokens": maxTokens,})}+ } else if bedrockAPIKeyAuth {+ common.ErrorWithCode(c, common.CodeServerError, err.Error())+ return}}}}+ if remoteFetched && bedrockAPIKeyAuth {+ if len(remoteModels) == 0 {+ common.ErrorWithCode(c, common.CodeDataError, "No Bedrock models were discovered")+ return+ }+ remoteNames := make(map[string]struct{}, len(remoteModels))+ for _, model := range remoteModels {+ if name, ok := model["name"].(string); ok {+ remoteNames[name] = struct{}{}+ }+ }+ filtered := staticModels[:0]+ for _, model := range staticModels {+ if name, ok := model["name"].(string); ok {+ if _, exists := remoteNames[name]; exists {+ filtered = append(filtered, model)+ }+ }+ }+ staticModels = filtered+ }// 3. Both empty — return empty successif len(staticModels) == 0 && len(remoteModels) == 0 {@@ -198,16 +245,24 @@ func (h *ProviderHandler) ListModels(c *gin.Context) {// 4. Merge: static as base, remote overrides on name conflictsmerged := make(map[string]map[string]interface{})for _, m := range staticModels {+ if maxTokens, ok := m["max_tokens"]; !ok || maxTokens == nil {+ if maxOutput, ok := m["max_output"]; ok && maxOutput != nil {+ m["max_tokens"] = maxOutput+ }+ }if name, ok := m["name"].(string); ok {merged[name] = m}}for _, m := range remoteModels {if name, ok := m["name"].(string); ok {- if len(providerModelMapTypes(m)) == 0 {- if existing := merged[name]; len(providerModelMapTypes(existing)) > 0 {+ if existing, exists := merged[name]; exists {+ if len(providerModelMapTypes(m)) == 0 && len(providerModelMapTypes(existing)) > 0 {m["model_types"] = existing["model_types"]}+ if maxTokens, ok := existing["max_tokens"]; ok && maxTokens != nil {+ m["max_tokens"] = maxTokens+ }}merged[name] = m}@@ -346,9 +401,9 @@ func (h *ProviderHandler) CreateProviderInstance(c *gin.Context) {return}- _, err := h.modelProviderService.CreateProviderInstance(ctx, providerName, req.InstanceName, apiKey, req.BaseURL, req.Region, userID, req.ModelInfo)+ code, err := h.modelProviderService.CreateProviderInstance(ctx, providerName, req.InstanceName, apiKey, req.BaseURL, req.Region, userID, req.ModelInfo)if err != nil {- common.ErrorWithCode(c, common.CodeServerError, err.Error())+ common.ErrorWithCode(c, code, err.Error())return}@@ -543,11 +598,11 @@ func (h *ProviderHandler) ShowTask(c *gin.Context) {}type AlterProviderInstanceRequest struct {- InstanceName string `json:"instance_name"`- APIKey json.RawMessage `json:"api_key"`- BaseURL string `json:"base_url"`- Region string `json:"region"`- ModelInfo []service.CreateInstanceModelInfo `json:"model_info"`+ InstanceName string `json:"instance_name"`+ APIKey json.RawMessage `json:"api_key"`+ BaseURL string `json:"base_url"`+ Region string `json:"region"`+ ModelInfo *[]service.CreateInstanceModelInfo `json:"model_info" binding:"required"`}func (h *ProviderHandler) AlterProviderInstance(c *gin.Context) {@@ -576,7 +631,7 @@ func (h *ProviderHandler) AlterProviderInstance(c *gin.Context) {return}- code, err := h.modelProviderService.AlterProviderInstance(ctx, userID, providerName, instanceName, req.InstanceName, normalizeAPIKey(req.APIKey), req.BaseURL, req.Region, req.ModelInfo)+ code, err := h.modelProviderService.AlterProviderInstance(ctx, userID, providerName, instanceName, req.InstanceName, normalizeAPIKey(req.APIKey), req.BaseURL, req.Region, *req.ModelInfo)if err != nil {common.ErrorWithCode(c, code, err.Error())returndiff --git a/internal/service/model_service.go b/internal/service/model_service.goindex 130cf450f2..3de0c33cbb 100644--- a/internal/service/model_service.go+++ b/internal/service/model_service.go@@ -535,6 +535,45 @@ type CreateInstanceModelInfo struct {Extra map[string]interface{} `json:"extra"`}+func validateBedrockAPIKeyAuth(providerName, apiKey string) (bool, string, error) {+ if !strings.EqualFold(providerName, "Bedrock") {+ return false, apiKey, nil+ }+ var rawConfig map[string]json.RawMessage+ if json.Unmarshal([]byte(apiKey), &rawConfig) != nil {+ return false, apiKey, nil+ }+ var authMode string+ if json.Unmarshal(rawConfig["auth_mode"], &authMode) != nil || authMode != "bedrock_api_key" {+ return false, apiKey, nil+ }+ var config struct {+ APIKey string `json:"bedrock_api_key"`+ Region string `json:"bedrock_region"`+ }+ if json.Unmarshal([]byte(apiKey), &config) != nil {+ return true, apiKey, errors.New("invalid Bedrock API-key configuration")+ }+ config.APIKey = strings.TrimSpace(config.APIKey)+ if config.APIKey == "" {+ return true, apiKey, errors.New("Bedrock API key must be provided")+ }+ config.Region = strings.TrimSpace(config.Region)+ if config.Region == "" {+ return true, apiKey, errors.New("AWS region must be provided")+ }+ if err := modelModule.ValidateBedrockRegion(config.Region); err != nil {+ return true, apiKey, err+ }+ rawConfig["bedrock_api_key"], _ = json.Marshal(config.APIKey)+ rawConfig["bedrock_region"], _ = json.Marshal(config.Region)+ normalizedAPIKey, err := json.Marshal(rawConfig)+ if err != nil {+ return true, apiKey, errors.New("invalid Bedrock API-key configuration")+ }+ return true, string(normalizedAPIKey), nil+}+func (m *ModelProviderService) getProviderByIDOrName(ctx context.Context, tenantID, providerIDOrName string) (*entity.TenantModelProvider, error) {provider, err := m.modelProviderDAO.GetByID(ctx, dao.DB, providerIDOrName)if err == nil && provider.TenantID == tenantID {@@ -545,6 +584,9 @@ func (m *ModelProviderService) getProviderByIDOrName(ctx context.Context, tenantfunc (m *ModelProviderService) CreateProviderInstance(ctx context.Context, providerIDOrName, instanceName, apiKey, baseURL, region, userID string, modelInfo []CreateInstanceModelInfo) (common.ErrorCode, error) {providerIDOrName = strings.TrimSpace(providerIDOrName)+ apiKey = strings.TrimSpace(apiKey)+ baseURL = strings.TrimSpace(baseURL)+ region = strings.TrimSpace(region)// Get tenant ID from usertenants, err := m.userTenantDAO.GetByUserIDAndRole(ctx, dao.DB, userID, "owner")@@ -570,6 +612,20 @@ func (m *ModelProviderService) CreateProviderInstance(ctx context.Context, proviapiKey = "x"}+ bedrockAPIKeyAuth, apiKey, err := validateBedrockAPIKeyAuth(providerName, apiKey)+ if err != nil {+ return common.CodeBadRequest, err+ }+ if bedrockAPIKeyAuth {+ if len(modelInfo) == 0 {+ return common.CodeBadRequest, errors.New("at least one Bedrock model must be selected")+ }+ for _, model := range modelInfo {+ if strings.TrimSpace(model.ModelName) == "" {+ return common.CodeBadRequest, errors.New("Bedrock model name must be provided")+ }+ }+ }instanceID := utility.GenerateToken()extra := make(map[string]string)@@ -601,7 +657,7 @@ func (m *ModelProviderService) CreateProviderInstance(ctx context.Context, provireturn common.CodeServerError, err}}- } else {+ } else if !bedrockAPIKeyAuth {// model_info not provided — add all factory default models.// Mirrors Python's create_provider_instance// (api/apps/services/provider_api_service.py:506-531).@@ -1913,6 +1969,9 @@ func (m *ModelProviderService) ensureOCRProviderFromEnv(ctx context.Context, ten}func (m *ModelProviderService) AlterProviderInstance(ctx context.Context, userID, providerIDOrName, instanceIDOrName, newInstanceName, apiKey, baseURL, region string, modelInfo []CreateInstanceModelInfo) (common.ErrorCode, error) {+ apiKey = strings.TrimSpace(apiKey)+ baseURL = strings.TrimSpace(baseURL)+ region = strings.TrimSpace(region)providerIDOrName = strings.TrimSpace(providerIDOrName)tenants, err := m.userTenantDAO.GetByUserIDAndRole(ctx, dao.DB, userID, "owner")@@ -1944,6 +2003,10 @@ func (m *ModelProviderService) AlterProviderInstance(ctx context.Context, userIDapiKey = "x"}+ _, apiKey, err = validateBedrockAPIKeyAuth(providerName, apiKey)+ if err != nil {+ return common.CodeBadRequest, err+ }// Update instance record.instanceUpdates := map[string]interface{}{"api_key": apiKey,diff --git a/internal/service/model_service_test.go b/internal/service/model_service_test.goindex 337d494025..efd3768372 100644--- a/internal/service/model_service_test.go+++ b/internal/service/model_service_test.go@@ -3,6 +3,7 @@ package serviceimport ("context""encoding/json"+ "errors""strings""testing"@@ -19,6 +20,8 @@ type remoteModelProbeDriver struct {*modelModule.DummyModelremoteModels []modelModule.ListModelResponseembedCalls int+ checkCalls int+ checkErr error}func (d *remoteModelProbeDriver) ListModels(context.Context, *modelModule.APIConfig) ([]modelModule.ListModelResponse, error) {@@ -30,6 +33,56 @@ func (d *remoteModelProbeDriver) Embed(context.Context, *string, modelModule.Embreturn nil, nil}+func (d *remoteModelProbeDriver) CheckConnection(context.Context, *modelModule.APIConfig) error {+ d.checkCalls+++ return d.checkErr+}++func TestValidateBedrockAPIKeyAuth(t *testing.T) {+ tests := []struct {+ name string+ apiKey string+ wantErr string+ }{+ {+ name: "rejects non-string API key",+ apiKey: `{"auth_mode":"bedrock_api_key","bedrock_api_key":[],"bedrock_region":"us-east-1"}`,+ wantErr: "invalid Bedrock API-key configuration",+ },+ {+ name: "rejects invalid region",+ apiKey: `{"auth_mode":"bedrock_api_key","bedrock_api_key":"test-key","bedrock_region":"us east 1"}`,+ wantErr: "invalid region",+ },+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ authenticated, _, err := validateBedrockAPIKeyAuth("Bedrock", test.apiKey)+ if !authenticated || err == nil || !strings.Contains(err.Error(), test.wantErr) {+ t.Fatalf("validateBedrockAPIKeyAuth() = (%v, %v), want API-key mode error containing %q", authenticated, err, test.wantErr)+ }+ })+ }++ nonAPIKey := `{"auth_mode":"access_key_secret","bedrock_region":"us-east-1"}`+ authenticated, normalized, err := validateBedrockAPIKeyAuth("Bedrock", nonAPIKey)+ if authenticated || err != nil || normalized != nonAPIKey {+ t.Fatalf("non-API-key mode = (%v, %q, %v), want unchanged", authenticated, normalized, err)+ }++ authenticated, normalized, err = validateBedrockAPIKeyAuth("Bedrock", `{"auth_mode":"bedrock_api_key","bedrock_api_key":" test-key ","bedrock_region":" us-east-1 ","custom":"value"}`)+ if !authenticated || err != nil {+ t.Fatalf("valid API-key mode = (%v, %v), want success", authenticated, err)+ }+ var config map[string]string+ if err = json.Unmarshal([]byte(normalized), &config); err != nil {+ t.Fatalf("decode normalized API key: %v", err)+ }+ if config["bedrock_api_key"] != "test-key" || config["bedrock_region"] != "us-east-1" || config["custom"] != "value" {+ t.Fatalf("normalized API key = %#v", config)+ }+}+func TestValidateEmbeddingModel(t *testing.T) {maxDimension := 2048maxBatchSize := 128@@ -272,6 +325,124 @@ func seedModelProviderServiceScope(t *testing.T, db *gorm.DB) {}}+func TestBedrockAPIKeyInstancePersistsDiscoveredModelsWithoutRuntimeVerification(t *testing.T) {+ db := setupModelProviderServiceTestDB(t)+ useModelProviderServiceTestDB(t, db)+ activeStatus := "1"+ for _, row := range []interface{}{+ &entity.UserTenant{ID: "user-tenant-bedrock", UserID: "user-1", TenantID: "tenant-bedrock", Role: "owner", InvitedBy: "user-1", Status: &activeStatus},+ &entity.TenantModelProvider{ID: "provider-bedrock", TenantID: "tenant-bedrock", ProviderName: "Bedrock"},+ } {+ if err := db.Create(row).Error; err != nil {+ t.Fatalf("failed to seed %T: %v", row, err)+ }+ }++ provider := dao.GetModelProviderManager().FindProvider("Bedrock")+ if provider == nil {+ t.Fatal("Bedrock provider is not configured")+ }+ probe := &remoteModelProbeDriver{+ DummyModel: modelModule.NewDummyModel(nil, modelModule.URLSuffix{}),+ checkErr: errors.New("runtime verification must not run"),+ }+ originalDriver := provider.ModelDriver+ provider.ModelDriver = probe+ t.Cleanup(func() { provider.ModelDriver = originalDriver })++ apiKey := `{"auth_mode":"bedrock_api_key","bedrock_api_key":"test-key","bedrock_region":"us-east-1"}`+ code, err := NewModelProviderService().CreateProviderInstance(+ t.Context(),+ "Bedrock",+ "api-key-instance",+ apiKey,+ "",+ "default",+ "user-1",+ []CreateInstanceModelInfo{{+ ModelName: "amazon.nova-lite-v1:0",+ ModelTypes: []string{"chat", "vision"},+ MaxTokens: 8192,+ }},+ )+ if err != nil {+ t.Fatalf("CreateProviderInstance() error = %v", err)+ }+ if code != common.CodeSuccess {+ t.Fatalf("code = %v, want %v", code, common.CodeSuccess)+ }+ if probe.checkCalls != 0 {+ t.Fatalf("CheckConnection calls = %d, want 0", probe.checkCalls)+ }++ var models []*entity.TenantModel+ if err = db.Find(&models).Error; err != nil {+ t.Fatalf("list models: %v", err)+ }+ if len(models) != 1 || models[0].ModelName != "amazon.nova-lite-v1:0" {+ t.Fatalf("models = %#v, want persisted Bedrock model", models)+ }+ code, err = NewModelProviderService().AlterProviderInstance(+ t.Context(),+ "user-1",+ "Bedrock",+ "api-key-instance",+ "api-key-instance",+ apiKey,+ "",+ "default",+ []CreateInstanceModelInfo{{+ ModelName: "amazon.nova-lite-v1:0",+ ModelTypes: []string{"chat", "vision"},+ MaxTokens: 8192,+ }},+ )+ if err != nil {+ t.Fatalf("AlterProviderInstance() error = %v", err)+ }+ if code != common.CodeSuccess {+ t.Fatalf("alter code = %v, want %v", code, common.CodeSuccess)+ }+ if probe.checkCalls != 0 {+ t.Fatalf("CheckConnection calls after alter = %d, want 0", probe.checkCalls)+ }++ code, err = NewModelProviderService().CreateProviderInstance(+ t.Context(),+ "Bedrock",+ "empty-api-key-instance",+ apiKey,+ "",+ "default",+ "user-1",+ nil,+ )+ if code != common.CodeBadRequest || err == nil {+ t.Fatalf("empty model list returned (%v, %v), want bad request", code, err)+ }++ code, err = NewModelProviderService().CreateProviderInstance(+ t.Context(),+ "Bedrock",+ "empty-model-name-instance",+ apiKey,+ "",+ "default",+ "user-1",+ []CreateInstanceModelInfo{{}},+ )+ if code != common.CodeBadRequest || err == nil {+ t.Fatalf("empty model name returned (%v, %v), want bad request", code, err)+ }+ var instanceCount int64+ if err = db.Model(&entity.TenantModelInstance{}).Where("instance_name = ?", "empty-model-name-instance").Count(&instanceCount).Error; err != nil {+ t.Fatalf("count instances: %v", err)+ }+ if instanceCount != 0 {+ t.Fatalf("empty model name created %d instances, want 0", instanceCount)+ }+}+func TestModelProviderServiceAlterModelStatusByID(t *testing.T) {db := setupModelProviderServiceTestDB(t)useModelProviderServiceTestDB(t, db)diff --git a/rag/llm/chat_model.py b/rag/llm/chat_model.pyindex 543387212d..2ba054f5c5 100644--- a/rag/llm/chat_model.py+++ b/rag/llm/chat_model.py@@ -2515,6 +2515,7 @@ class LiteLLMBase(ABC):completion_args.update({"api_base": self.base_url})elif self.provider == SupportedLiteLLMProvider.Bedrock:import boto3+ from botocore.utils import validate_region_namecompletion_args.pop("api_key", None)completion_args.pop("api_base", None)@@ -2526,6 +2527,9 @@ class LiteLLMBase(ABC):raise ValueError("Bedrock auth_mode must be provided in the key")bedrock_region = bedrock_key.get("bedrock_region")+ if not bedrock_region:+ raise ValueError("Bedrock region must be provided in the key")+ validate_region_name(bedrock_region)if mode == "access_key_secret":completion_args.update({"aws_region_name": bedrock_region})@@ -2540,8 +2544,20 @@ class LiteLLMBase(ABC):completion_args.update({"aws_access_key_id": creds["AccessKeyId"]})completion_args.update({"aws_secret_access_key": creds["SecretAccessKey"]})completion_args.update({"aws_session_token": creds["SessionToken"]})- else: # assume_role - use default credential chain (IRSA, instance profile, etc.)+ elif mode == "assume_role":completion_args.update({"aws_region_name": bedrock_region})+ elif mode == "bedrock_api_key":+ api_key = bedrock_key.get("bedrock_api_key")+ if not api_key:+ raise ValueError("Bedrock API key must be provided")+ completion_args.update(+ {+ "api_key": api_key,+ "aws_region_name": bedrock_region,+ }+ )+ else:+ raise ValueError(f"Unsupported Bedrock auth_mode: {mode}")elif self.provider == SupportedLiteLLMProvider.OpenRouter:if self.provider_order:diff --git a/rag/llm/cv_model.py b/rag/llm/cv_model.pyindex df22c41083..83c7845121 100644--- a/rag/llm/cv_model.py+++ b/rag/llm/cv_model.py@@ -1372,14 +1372,27 @@ class BedrockCV(Base):Base.__init__(self, **kwargs)def _parse_credentials(self, key):+ from botocore.utils import validate_region_name+bedrock_key = json.loads(key)self.auth_mode = bedrock_key.get("auth_mode", "")- self.aws_region = bedrock_key.get("bedrock_region", "us-east-1")+ self.aws_region = bedrock_key.get("bedrock_region")+ if not self.aws_region:+ raise ValueError("Bedrock region must be provided in the key")+ validate_region_name(self.aws_region)self.aws_ak = bedrock_key.get("bedrock_ak", "")self.aws_sk = bedrock_key.get("bedrock_sk", "")self.aws_role_arn = bedrock_key.get("aws_role_arn", "")+ self.bedrock_api_key = bedrock_key.get("bedrock_api_key", "")def _get_aws_creds(self):+ if self.auth_mode == "bedrock_api_key":+ if not self.bedrock_api_key:+ raise ValueError("Bedrock API key must be provided")+ return {+ "aws_region_name": self.aws_region,+ "api_key": self.bedrock_api_key,+ }if self.auth_mode == "access_key_secret":return {"aws_region_name": self.aws_region,@@ -1398,8 +1411,9 @@ class BedrockCV(Base):"aws_secret_access_key": creds["SecretAccessKey"],"aws_session_token": creds["SessionToken"],}- else:+ elif self.auth_mode == "assume_role":return {"aws_region_name": self.aws_region}+ raise ValueError(f"Unsupported Bedrock auth_mode: {self.auth_mode}")def describe_with_prompt(self, image, prompt=None):import litellmdiff --git a/rag/llm/embedding_model.py b/rag/llm/embedding_model.pyindex 3e4e58f320..2953f14593 100644--- a/rag/llm/embedding_model.py+++ b/rag/llm/embedding_model.py@@ -665,6 +665,10 @@ class BedrockEmbed(Base):def __init__(self, key, model_name, **kwargs):import boto3+ from botocore import UNSIGNED+ from botocore.awsrequest import AWSRequest+ from botocore.config import Config+ from botocore.utils import validate_region_name# `key` protocol (backend stores as JSON string in `api_key`):# - Must decode into a dict.@@ -672,7 +676,8 @@ class BedrockEmbed(Base):# - Supported auth modes:# - "access_key_secret": requires `bedrock_ak` + `bedrock_sk`.# - "iam_role": requires `aws_role_arn` and assumes role via STS.- # - else: treated as "assume_role" (default AWS credential chain).+ # - "assume_role": uses the default AWS credential chain.+ # - "bedrock_api_key": uses a request-scoped Bearer token.key = json.loads(key)mode = key.get("auth_mode")if not mode:@@ -680,12 +685,29 @@ class BedrockEmbed(Base):raise ValueError("Bedrock auth_mode must be provided in the key")self.bedrock_region = key.get("bedrock_region")+ if not self.bedrock_region:+ raise ValueError("Bedrock region must be provided in the key")+ validate_region_name(self.bedrock_region)self.model_name = model_nameself.is_amazon = self.model_name.split(".")[0] == "amazon"self.is_cohere = self.model_name.split(".")[0] == "cohere"- if mode == "access_key_secret":+ if mode == "bedrock_api_key":+ api_key = key.get("bedrock_api_key")+ if not api_key:+ raise ValueError("Bedrock API key must be provided")+ self.client = boto3.client(+ service_name="bedrock-runtime",+ region_name=self.bedrock_region,+ config=Config(signature_version=UNSIGNED),+ )++ def add_bearer_token(request: AWSRequest, **_kwargs: object) -> None:+ request.headers["Authorization"] = f"Bearer {api_key}"++ self.client.meta.events.register("before-sign.bedrock-runtime.*", add_bearer_token)+ elif mode == "access_key_secret":self.bedrock_ak = key.get("bedrock_ak")self.bedrock_sk = key.get("bedrock_sk")self.client = boto3.client(service_name="bedrock-runtime", region_name=self.bedrock_region, aws_access_key_id=self.bedrock_ak, aws_secret_access_key=self.bedrock_sk)@@ -701,8 +723,10 @@ class BedrockEmbed(Base):aws_secret_access_key=creds["SecretAccessKey"],aws_session_token=creds["SessionToken"],)- else: # assume_role+ elif mode == "assume_role":self.client = boto3.client("bedrock-runtime", region_name=self.bedrock_region)+ else:+ raise ValueError(f"Unsupported Bedrock auth_mode: {mode}")def _extract_vector(self, model_response):# Titan returns {"embedding": [...]}; Cohere returns {"embeddings": [[...]]}.diff --git a/rag/llm/model_meta.py b/rag/llm/model_meta.pyindex 970e438f40..1bddce0198 100644--- a/rag/llm/model_meta.py+++ b/rag/llm/model_meta.py@@ -13,14 +13,21 @@# See the License for the specific language governing permissions and# limitations under the License.#+import asyncioimport jsonimport logging-import aiohttpfrom abc import ABCfrom datetime import datetime, timezone-from urllib.parse import urlparsefrom json.decoder import JSONDecodeErrorfrom typing import ClassVar+from urllib.parse import urlparse++import aiohttp+from botocore import UNSIGNED+from botocore.awsrequest import AWSRequest+from botocore.config import Config+from botocore.exceptions import BotoCoreError, ClientError+from botocore.utils import validate_region_namefrom common.aimlapi_utils import attribution_headersfrom common.constants import LLMType@@ -97,6 +104,86 @@ class Base(ABC):return self._format_model_list(raw_model_list)+class Bedrock(Base):+ _FACTORY_NAME = "Bedrock"++ def _get_config(self) -> tuple[str, str]:+ config = self.api_key+ if isinstance(config, str):+ try:+ config = json.loads(config)+ except JSONDecodeError as error:+ raise ValueError("Bedrock credentials must be a JSON object") from error+ if not isinstance(config, dict) or config.get("auth_mode") != "bedrock_api_key":+ raise ValueError("Bedrock API key authentication is required to list models")++ api_key = config.get("bedrock_api_key")+ if not isinstance(api_key, str) or not api_key.strip():+ raise ValueError("Bedrock API key must be provided")+ region = config.get("bedrock_region")+ if not isinstance(region, str) or not region.strip():+ raise ValueError("Bedrock region must be provided in the key")+ validate_region_name(region)+ return api_key.strip(), region++ def _list_foundation_models(self, api_key: str, region: str) -> dict[str, object]:+ import boto3++ client = boto3.client(+ service_name="bedrock",+ region_name=region,+ config=Config(+ signature_version=UNSIGNED,+ connect_timeout=10,+ read_timeout=10,+ retries={"mode": "standard", "max_attempts": 0},+ ),+ )++ def add_bearer_token(request: AWSRequest, **_kwargs: object) -> None:+ request.headers["Authorization"] = f"Bearer {api_key}"++ client.meta.events.register("before-sign.bedrock.*", add_bearer_token)+ return client.list_foundation_models(byInferenceType="ON_DEMAND")++ def _format_model_list(self, raw_model_list: dict[str, object]) -> list[dict[str, object]]:+ models: list[dict[str, object]] = []+ for summary in raw_model_list.get("modelSummaries", []):+ if not isinstance(summary, dict):+ continue+ model_id = summary.get("modelId")+ input_modalities = summary.get("inputModalities", [])+ output_modalities = summary.get("outputModalities", [])+ inference_types = summary.get("inferenceTypesSupported", [])+ lifecycle_status = (summary.get("modelLifecycle") or {}).get("status")+ if not model_id or "TEXT" not in input_modalities or "rerank" in model_id.lower():+ continue+ if inference_types and "ON_DEMAND" not in inference_types:+ continue+ if lifecycle_status and lifecycle_status != "ACTIVE":+ continue+ if "EMBEDDING" in output_modalities and model_id.startswith(("amazon.titan-embed-text", "cohere.embed-")):+ model_types = [LLMType.EMBEDDING.value]+ elif "TEXT" in output_modalities:+ model_types = [LLMType.CHAT.value]+ if "IMAGE" in input_modalities:+ model_types.append(LLMType.VISION.value)+ else:+ continue+ models.append({"name": model_id, "model_types": model_types, "max_tokens": 8192, "features": []})+ return models++ async def get_model_list(self) -> list[dict[str, object]]:+ api_key, region = self._get_config()+ try:+ raw_model_list = await asyncio.to_thread(self._list_foundation_models, api_key, region)+ except ClientError as error:+ raise ValueError(f"Failed to list models from Amazon Bedrock in region '{region}'") from error+ except BotoCoreError as error:+ raise ValueError("Failed to list models from Amazon Bedrock") from error+ return self._format_model_list(raw_model_list)++class VolcEngine(Base):_FACTORY_NAME = "VolcEngine"diff --git a/rag/llm/rerank_model.py b/rag/llm/rerank_model.pyindex a14a9a97fa..4a09c104dd 100644--- a/rag/llm/rerank_model.py+++ b/rag/llm/rerank_model.py@@ -434,6 +434,7 @@ class BedrockRerank(Base):def __init__(self, key, model_name, **kwargs):import boto3+ from botocore.utils import validate_region_namekey = json.loads(key)mode = key.get("auth_mode")@@ -442,6 +443,9 @@ class BedrockRerank(Base):raise ValueError("Bedrock auth_mode must be provided in the key")self.bedrock_region = key.get("bedrock_region")+ if not self.bedrock_region:+ raise ValueError("Bedrock region must be provided in the key")+ validate_region_name(self.bedrock_region)self.model_name = model_name# On-demand foundation-model ARN; works for amazon.rerank-v1:0 / cohere.rerank-*.self.model_arn = f"arn:aws:bedrock:{self.bedrock_region}::foundation-model/{self.model_name}"@@ -470,8 +474,12 @@ class BedrockRerank(Base):aws_secret_access_key=creds["SecretAccessKey"],aws_session_token=creds["SessionToken"],)- else: # assume_role: default AWS credential chain+ elif mode == "assume_role":self.client = boto3.client("bedrock-agent-runtime", region_name=self.bedrock_region)+ elif mode == "bedrock_api_key":+ raise ValueError("Bedrock API key authentication is not supported for rerank")+ else:+ raise ValueError(f"Unsupported Bedrock auth_mode: {mode}")def _compute_rank(self, query: str, texts: List) -> Tuple[np.ndarray, int]:# Truncate to the model token window, then enforce the API's hard 32k-chardiff --git a/test/unit_test/rag/llm/conftest.py b/test/unit_test/rag/llm/conftest.pyindex 60ed0a9965..7ac4c5e9bf 100644--- a/test/unit_test/rag/llm/conftest.py+++ b/test/unit_test/rag/llm/conftest.py@@ -61,6 +61,11 @@ def _install_rag_llm_stub():Tongyi_Qianwen = "Tongyi-Qianwen"Dashscope = "Dashscope"Moonshot = "Moonshot"+ Nvidia = "NVIDIA"+ Bedrock = "Bedrock"+ Ollama = "Ollama"+ OpenRouter = "OpenRouter"+ MiniMax = "MiniMax"ZHIPU_AI = "ZHIPU-AI"OpenAI = "OpenAI"Azure_OpenAI = "Azure-OpenAI"@@ -68,7 +73,7 @@ def _install_rag_llm_stub():llm_pkg.SupportedLiteLLMProvider = SupportedLiteLLMProviderllm_pkg.FACTORY_DEFAULT_BASE_URL = {}- llm_pkg.LITELLM_PROVIDER_PREFIX = {}+ llm_pkg.LITELLM_PROVIDER_PREFIX = {SupportedLiteLLMProvider.Bedrock: "bedrock/"}sys.modules["rag.llm"] = llm_pkgdiff --git a/test/unit_test/rag/llm/test_bedrock_api_key_auth.py b/test/unit_test/rag/llm/test_bedrock_api_key_auth.pynew file mode 100644index 0000000000..90d8b0d7e0--- /dev/null+++ b/test/unit_test/rag/llm/test_bedrock_api_key_auth.py@@ -0,0 +1,100 @@+#+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+#++import json+from types import SimpleNamespace+from unittest.mock import MagicMock, patch++import pytest+from botocore import UNSIGNED++from rag.llm import SupportedLiteLLMProvider+from rag.llm.chat_model import LiteLLMBase+from rag.llm.cv_model import BedrockCV+from rag.llm.embedding_model import BedrockEmbed+++class _BedrockChat(LiteLLMBase):+ pass+++def _api_key_config(**overrides: str) -> str:+ config = {+ "auth_mode": "bedrock_api_key",+ "bedrock_region": "us-east-1",+ "bedrock_api_key": "bedrock-test-key",+ **overrides,+ }+ return json.dumps(config)+++def _chat_model(api_key: str) -> _BedrockChat:+ return _BedrockChat(+ api_key,+ "amazon.nova-lite-v1:0",+ provider=SupportedLiteLLMProvider.Bedrock,+ max_retries=5,+ )+++def test_bedrock_chat_uses_request_scoped_bearer_token():+ model = _chat_model(_api_key_config())+ args = model._construct_completion_args(+ [{"role": "user", "content": "Hi"}],+ stream=False,+ tools=False,+ )++ assert model.model_name == "bedrock/amazon.nova-lite-v1:0"+ assert args["aws_region_name"] == "us-east-1"+ assert args["api_key"] == "bedrock-test-key"+ assert "aws_access_key_id" not in args+ assert "aws_secret_access_key" not in args+++def test_bedrock_chat_requires_api_key_value():+ with pytest.raises(ValueError, match="Bedrock API key must be provided"):+ _chat_model(_api_key_config(bedrock_api_key=""))._construct_completion_args(+ [{"role": "user", "content": "Hi"}],+ stream=False,+ tools=False,+ )+++def test_bedrock_embedding_uses_unsigned_client_with_bearer_token():+ client = MagicMock()+ with patch("boto3.client", return_value=client) as client_factory:+ BedrockEmbed(_api_key_config(), "amazon.titan-embed-text-v2:0")++ kwargs = client_factory.call_args.kwargs+ assert kwargs["service_name"] == "bedrock-runtime"+ assert kwargs["region_name"] == "us-east-1"+ assert kwargs["config"].signature_version == UNSIGNED++ event_name, add_bearer_token = client.meta.events.register.call_args.args+ request = SimpleNamespace(headers={})+ add_bearer_token(request)+ assert event_name == "before-sign.bedrock-runtime.*"+ assert request.headers["Authorization"] == "Bearer bedrock-test-key"+++def test_bedrock_vision_uses_request_scoped_bearer_token():+ args = BedrockCV(_api_key_config(), "amazon.nova-lite-v1:0")._get_aws_creds()++ assert args["aws_region_name"] == "us-east-1"+ assert args["api_key"] == "bedrock-test-key"+ assert "aws_access_key_id" not in args+ assert "aws_secret_access_key" not in argsdiff --git a/test/unit_test/rag/llm/test_bedrock_rerank.py b/test/unit_test/rag/llm/test_bedrock_rerank.pyindex 777308286b..68a8430baf 100644--- a/test/unit_test/rag/llm/test_bedrock_rerank.py+++ b/test/unit_test/rag/llm/test_bedrock_rerank.py@@ -94,9 +94,13 @@ def test_number_of_results_covers_all_documents():def test_missing_auth_mode_raises():- with patch("boto3.client"):- with pytest.raises(ValueError):- BedrockRerank(json.dumps({"bedrock_region": "eu-central-1"}), "amazon.rerank-v1:0")+ with patch("boto3.client"), pytest.raises(ValueError):+ BedrockRerank(json.dumps({"bedrock_region": "eu-central-1"}), "amazon.rerank-v1:0")+++def test_missing_region_raises_before_building_model_arn():+ with pytest.raises(ValueError, match="region must be provided"):+ BedrockRerank(json.dumps({"auth_mode": "assume_role"}), "amazon.rerank-v1:0")def test_access_key_secret_mode_wires_the_client():@@ -110,6 +114,18 @@ def test_access_key_secret_mode_wires_the_client():assert kwargs["aws_secret_access_key"] == "secret_test"+def test_bedrock_api_key_mode_is_rejected_for_rerank():+ key = json.dumps(+ {+ "auth_mode": "bedrock_api_key",+ "bedrock_region": "eu-central-1",+ "bedrock_api_key": "bedrock-test-key",+ }+ )+ with pytest.raises(ValueError, match="not supported for rerank"):+ BedrockRerank(key, "amazon.rerank-v1:0")++@pytest.mark.parametrize("query, texts", [("", ["a"]), ("q", [])])def test_empty_input_short_circuits_without_calling_bedrock(query, texts):mdl, client = _make()diff --git a/test/unit_test/rag/llm/test_model_meta_bedrock.py b/test/unit_test/rag/llm/test_model_meta_bedrock.pynew file mode 100644index 0000000000..397e3eff97--- /dev/null+++ b/test/unit_test/rag/llm/test_model_meta_bedrock.py@@ -0,0 +1,116 @@+#+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+#++import asyncio+from types import SimpleNamespace+from unittest.mock import MagicMock, patch++import pytest+from botocore import UNSIGNED+from botocore.exceptions import ClientError++from common.constants import LLMType+from rag.llm.model_meta import Bedrock++pytestmark = pytest.mark.p2+++def test_bedrock_lists_supported_on_demand_models_with_bearer_token():+ client = MagicMock()+ client.list_foundation_models.return_value = {+ "modelSummaries": [+ {+ "modelId": "amazon.nova-lite-v1:0",+ "inputModalities": ["TEXT", "IMAGE"],+ "outputModalities": ["TEXT"],+ "inferenceTypesSupported": ["ON_DEMAND"],+ "modelLifecycle": {"status": "ACTIVE"},+ },+ {+ "modelId": "amazon.titan-embed-text-v2:0",+ "inputModalities": ["TEXT"],+ "outputModalities": ["EMBEDDING"],+ "inferenceTypesSupported": ["ON_DEMAND"],+ },+ {+ "modelId": "cohere.rerank-v3-5:0",+ "inputModalities": ["TEXT"],+ "outputModalities": ["TEXT"],+ },+ ]+ }+ config = {+ "auth_mode": "bedrock_api_key",+ "bedrock_api_key": "bedrock-test-key",+ "bedrock_region": "us-east-1",+ }++ with patch("boto3.client", return_value=client) as client_factory:+ models = asyncio.run(Bedrock(config).get_model_list())++ kwargs = client_factory.call_args.kwargs+ assert kwargs["service_name"] == "bedrock"+ assert kwargs["region_name"] == "us-east-1"+ assert kwargs["config"].signature_version == UNSIGNED+ event_name, add_bearer_token = client.meta.events.register.call_args.args+ request = SimpleNamespace(headers={})+ add_bearer_token(request)+ assert event_name == "before-sign.bedrock.*"+ assert request.headers["Authorization"] == "Bearer bedrock-test-key"+ assert models == [+ {+ "name": "amazon.nova-lite-v1:0",+ "model_types": [LLMType.CHAT.value, LLMType.VISION.value],+ "max_tokens": 8192,+ "features": [],+ },+ {+ "name": "amazon.titan-embed-text-v2:0",+ "model_types": [LLMType.EMBEDDING.value],+ "max_tokens": 8192,+ "features": [],+ },+ ]+++def test_bedrock_requires_api_key_for_model_discovery():+ config = {+ "auth_mode": "bedrock_api_key",+ "bedrock_api_key": "",+ "bedrock_region": "us-east-1",+ }++ with pytest.raises(ValueError, match="Bedrock API key must be provided"):+ asyncio.run(Bedrock(config).get_model_list())+++def test_bedrock_model_discovery_error_includes_region():+ client = MagicMock()+ client.list_foundation_models.side_effect = ClientError(+ {+ "Error": {"Code": "AccessDeniedException", "Message": "denied"},+ "ResponseMetadata": {"HTTPStatusCode": 403},+ },+ "ListFoundationModels",+ )+ config = {+ "auth_mode": "bedrock_api_key",+ "bedrock_api_key": "bedrock-test-key",+ "bedrock_region": "us-east-1",+ }++ with patch("boto3.client", return_value=client), pytest.raises(ValueError, match="region 'us-east-1'"):+ asyncio.run(Bedrock(config).get_model_list())diff --git a/web/src/hooks/__tests__/use-add-provider-instance.test.tsx b/web/src/hooks/__tests__/use-add-provider-instance.test.tsxnew file mode 100644index 0000000000..deb363892f--- /dev/null+++ b/web/src/hooks/__tests__/use-add-provider-instance.test.tsx@@ -0,0 +1,114 @@+/*+ * Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { QueryClient, QueryClientProvider } from '@tanstack/react-query';+import { act, renderHook, waitFor } from '@testing-library/react';++import {+ LlmKeys,+ useAddProviderInstance,+ useFetchInstanceModels,+} from '../use-llm-request';++const mockListProviders = jest.fn();+const mockListProviderInstances = jest.fn();+const mockAddProviderInstance = jest.fn();+const mockListInstanceModels = jest.fn();++jest.mock('@/services/llm-service', () => ({+ __esModule: true,+ default: {+ listProviders: (...args: unknown[]) => mockListProviders(...args),+ listProviderInstances: (...args: unknown[]) =>+ mockListProviderInstances(...args),+ addProviderInstance: (...args: unknown[]) =>+ mockAddProviderInstance(...args),+ listInstanceModels: (...args: unknown[]) => mockListInstanceModels(...args),+ },+}));++describe('useFetchInstanceModels', () => {+ it('rejects a non-zero response code as a failed snapshot', async () => {+ mockListInstanceModels.mockResolvedValue({+ data: { code: 102, data: null, message: 'failed to list models' },+ });+ const queryClient = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ });+ const wrapper = ({ children }: { children: React.ReactNode }) => (+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>+ );++ const { result } = renderHook(+ () => useFetchInstanceModels('Bedrock', 'saved-instance'),+ { wrapper },+ );++ await waitFor(() => expect(result.current.loading).toBe(false));+ expect(result.current.isSuccess).toBe(false);+ });+});++jest.mock('../use-warn-empty-model', () => ({+ useWarnEmptyModel: jest.fn(),+}));++describe('useAddProviderInstance', () => {+ it('invalidates the added-model catalog after creating an instance', async () => {+ mockListProviders.mockResolvedValue({+ data: { code: 0, data: [{ name: 'Bedrock' }] },+ });+ mockListProviderInstances.mockResolvedValue({+ data: { code: 0, data: [] },+ });+ mockAddProviderInstance.mockResolvedValue({+ data: { code: 0, data: null },+ });++ const queryClient = new QueryClient({+ defaultOptions: { mutations: { retry: false } },+ });+ const invalidateQueries = jest.spyOn(queryClient, 'invalidateQueries');+ const wrapper = ({ children }: { children: React.ReactNode }) => (+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>+ );+ const { result } = renderHook(() => useAddProviderInstance(), { wrapper });++ await act(async () => {+ await result.current.addProviderInstance({+ llm_factory: 'Bedrock',+ instance_name: 'bedrock-api-key',+ api_key: {+ auth_mode: 'bedrock_api_key',+ bedrock_api_key: 'test-key',+ bedrock_region: 'us-east-1',+ },+ max_tokens: 8192,+ model_info: [+ {+ model_name: 'amazon.nova-lite-v1:0',+ model_type: ['chat'],+ max_tokens: 8192,+ },+ ],+ });+ });++ expect(invalidateQueries).toHaveBeenCalledWith({+ queryKey: LlmKeys.allModels(),+ });+ });+});diff --git a/web/src/hooks/use-llm-request.tsx b/web/src/hooks/use-llm-request.tsxindex 34e40bdacd..ff9976ba41 100644--- a/web/src/hooks/use-llm-request.tsx+++ b/web/src/hooks/use-llm-request.tsx@@ -229,7 +229,11 @@ export const useFetchInstanceModels = (providerName: string,instanceName: string,) => {- const { data, isFetching: loading } = useQuery<IInstanceModel[]>({+ const {+ data,+ isFetching: loading,+ isSuccess,+ } = useQuery<IInstanceModel[]>({queryKey: LlmKeys.instanceModels(providerName, instanceName),initialData: [],gcTime: 0,@@ -239,11 +243,14 @@ export const useFetchInstanceModels = ({ provider_name: providerName, instance_name: instanceName },true,);+ if (data?.code !== 0) {+ throw new Error(data?.message || 'Failed to fetch instance models');+ }return data?.data ?? [];},});- return { data, loading };+ return { data, loading, isSuccess };};export type LlmItem = { name: string; logo: string } & IMyLlmValue;@@ -293,20 +300,15 @@ export const useAddProviderInstance = () => {try {await addProvider({ provider_name: params.llm_factory });- // When `id` is supplied the caller is updating an existing- // instance (blur-save on a saved card), so do not short-circuit- // on the "already exists" check — we *want* the server call.- if (!params.id) {- const { data: instancesRes } = await llmService.listProviderInstances(- { provider_name: params.llm_factory },- true,- );- const instanceExists = instancesRes?.data?.some(- (i: IProviderInstance) => i.instance_name === params.instance_name,- );- if (instanceExists && !params.verify) {- return { code: 0, data: null };- }+ const { data: instancesRes } = await llmService.listProviderInstances(+ { provider_name: params.llm_factory },+ true,+ );+ const instanceExists = instancesRes?.data?.some(+ (i: IProviderInstance) => i.instance_name === params.instance_name,+ );+ if (instanceExists && !params.verify) {+ return { code: 0, data: null };}} catch {// ignore list failure and proceed to add@@ -338,6 +340,9 @@ export const useAddProviderInstance = () => {queryClient.invalidateQueries({queryKey: LlmKeys.providerInstances(params.llm_factory),});+ queryClient.invalidateQueries({+ queryKey: LlmKeys.allModels(),+ });}return data;},diff --git a/web/src/interfaces/request/llm.ts b/web/src/interfaces/request/llm.tsindex 615deb31c4..55db0175eb 100644--- a/web/src/interfaces/request/llm.ts+++ b/web/src/interfaces/request/llm.ts@@ -38,13 +38,6 @@ export interface IAddProviderRequestBody {export type IAddProviderInstanceRequestBody = IAddLlmRequestBody & {instance_name: string;region?: string;- /**- * Optional id of an existing instance. When present the backend- * treats the call as an update of that row (rather than a create),- * which is what the inline "blur-save" flow on saved instance cards- * needs.- */- id?: string;};export interface IDeleteProviderInstanceRequestBody {@@ -154,7 +147,7 @@ export interface IProviderModelItem {*/export interface IListProviderModelsRequestBody {provider_name: string;- api_key?: string;+ api_key?: string | object;base_url?: string;region?: string;model_info?: IModelInfo[];diff --git a/web/src/locales/en.ts b/web/src/locales/en.tsindex 4c7e9c4861..c40bdec028 100644--- a/web/src/locales/en.ts+++ b/web/src/locales/en.ts@@ -2239,6 +2239,8 @@ Example: Virtual Hosted Style`,listModelsLoading: 'Loading models…',selectModelBeforeVerify:'Please select at least one model before verification.',+ selectModelBeforeSave:+ 'Please discover and select at least one model before saving.',addCustomModel: 'Add custom model',addCustomModelTitle: 'Add custom model',batchAddModels: 'Add all models',diff --git a/web/src/locales/fr.ts b/web/src/locales/fr.tsindex 840ac1abae..2f072734bd 100644--- a/web/src/locales/fr.ts+++ b/web/src/locales/fr.ts@@ -1815,6 +1815,8 @@ Exemple : Virtual Hosted Style`,listModelsLoading: 'Chargement des modèles…',selectModelBeforeVerify:'Veuillez sélectionner au moins un modèle avant la vérification.',+ selectModelBeforeSave:+ 'Veuillez découvrir et sélectionner au moins un modèle avant l’enregistrement.',addCustomModel: 'Ajouter un modèle personnalisé',addCustomModelTitle: 'Ajouter un modèle personnalisé',editCustomModelTitle: 'Modifier le modèle',diff --git a/web/src/locales/ja.ts b/web/src/locales/ja.tsindex c6db695887..2e19ecc2de 100644--- a/web/src/locales/ja.ts+++ b/web/src/locales/ja.ts@@ -1777,6 +1777,8 @@ export default {listModelsLoading: 'モデルを読み込み中…',selectModelBeforeVerify:'検証する前に、少なくとも1つのモデルを選択してください。',+ selectModelBeforeSave:+ '保存する前に、少なくとも1つのモデルを検出して選択してください。',addCustomModel: 'カスタムモデルを追加',addCustomModelTitle: 'カスタムモデルを追加',batchAddModels: '表示中のモデルをすべて追加',diff --git a/web/src/locales/ko.ts b/web/src/locales/ko.tsindex 612a598385..3fd907fcce 100644--- a/web/src/locales/ko.ts+++ b/web/src/locales/ko.ts@@ -1760,6 +1760,8 @@ export default {listModelsEmpty: '사용 가능한 모델 없음',listModelsLoading: '모델 로딩 중…',selectModelBeforeVerify: '검증 전 모델을 최소 하나 이상 선택해 주세요.',+ selectModelBeforeSave:+ '저장하기 전에 모델을 하나 이상 검색하고 선택해 주세요.',addCustomModel: '커스텀 모델 추가',addCustomModelTitle: '커스텀 모델 추가',editCustomModelTitle: '모델 편집',diff --git a/web/src/locales/zh.ts b/web/src/locales/zh.tsindex 8d0fb9af5a..721c738b62 100644--- a/web/src/locales/zh.ts+++ b/web/src/locales/zh.ts@@ -1875,6 +1875,7 @@ NER:使用 spaCy NER 和基于规则的关键词提取来抽取 Entities 和 RlistModelsEmpty: '暂无可用模型',listModelsLoading: '正在加载模型…',selectModelBeforeVerify: '请至少选择一个模型后再验证。',+ selectModelBeforeSave: '请先发现并选择至少一个模型后再保存。',addCustomModel: '添加自定义模型',addCustomModelTitle: '添加自定义模型',batchAddModels: '添加全部模型',diff --git a/web/src/pages/user-setting/setting-model/index.tsx b/web/src/pages/user-setting/setting-model/index.tsxindex 2f09bd6117..c4cefbe1ef 100644--- a/web/src/pages/user-setting/setting-model/index.tsx+++ b/web/src/pages/user-setting/setting-model/index.tsx@@ -49,8 +49,8 @@ import SystemSetting from './layout/system-setting';* Save flow: the top Save button validates every visible card through* the imperative ref API; if all are valid it collects each card's* payload (skipping non-dirty saved cards) and dispatches one API call- * per dirty card - `addProviderInstance` for drafts and Bedrock- * saved cards, `updateProviderInstance` for generic saved cards.+ * per dirty card - `addProviderInstance` for drafts and+ * `updateProviderInstance` for saved cards.** Special-case providers (handled inside `ProviderInstanceCard`):* - `Bedrock`: rendered inline via `BedrockInstanceCard`.@@ -163,11 +163,9 @@ const SettingModelV2: FC = () => {// when not dirty so we skip the redundant API call.// 3. If any card is invalid (or a draft has no name), abort the// whole batch - errors are surfaced in the form UI by `trigger()`.- // 4. Dispatch one API call per dirty card, in order. Drafts and- // Bedrock saved cards go through `addProviderInstance`- // (Bedrock saved cards carry an `id` so the backend- // updates instead of creating); generic and SoMark saved cards- // go through `updateProviderInstance`.+ // 4. Dispatch one API call per dirty card, in order. Drafts go through+ // `addProviderInstance`; saved cards go through+ // `updateProviderInstance`.// 5. On success: clear drafts (they're persisted now), mark each// saved card's baseline so the next save short-circuits, and// invalidate the instance query so the new/updated cards appear.diff --git a/web/src/pages/user-setting/setting-model/instance-card/bedrock-instance-card.test.tsx b/web/src/pages/user-setting/setting-model/instance-card/bedrock-instance-card.test.tsxnew file mode 100644index 0000000000..e9a5a002d7--- /dev/null+++ b/web/src/pages/user-setting/setting-model/instance-card/bedrock-instance-card.test.tsx@@ -0,0 +1,376 @@+/*+ * Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { fireEvent, render, screen, waitFor } from '@testing-library/react';+import { createRef } from 'react';++import { BedrockInstanceCard } from './bedrock-instance-card';++type TestCardRef = {+ validate: () => Promise<boolean>;+ getSavePayload: () => {+ apiKind: 'add' | 'update';+ instanceName: string;+ isDraft: boolean;+ payload: Record<string, unknown>;+ } | null;+ markSaved: () => void;+};++const mockCatalogCredentials = jest.fn();+const mockFetchProviderInstance = jest.fn();+const mockRefetchProviderInstance = jest.fn();+type MockModelInfo = {+ model_name: string;+ model_type: string[];+ max_tokens: number;+};+let mockDiscoveredModels: MockModelInfo[];+let mockInstanceModelsLoaded: boolean;+const savedInstanceDetails = {+ id: 'instance-id',+ instance_name: 'saved-instance',+ provider_id: 'provider-id',+ region: 'us-east-1',+ status: 'active',+ api_key: JSON.stringify({+ auth_mode: 'bedrock_api_key',+ bedrock_api_key: 'old-key',+ bedrock_region: 'us-east-1',+ }),+};++jest.mock('react-i18next', () => ({+ useTranslation: () => ({ t: (key: string) => key }),+}));++jest.mock('@/hooks/common-hooks', () => ({+ useTranslate: () => ({ t: (key: string) => key }),+}));++jest.mock('@/hooks/logic-hooks/use-build-options', () => ({+ useBuildModelTypeOptions: () => ({ buildModelTypeOptions: jest.fn() }),+}));++jest.mock('@/hooks/use-llm-request', () => {+ return {+ useDeleteProviderInstance: () => ({ deleteProviderInstance: jest.fn() }),+ useFetchProviderInstance: (...args: unknown[]) =>+ mockFetchProviderInstance(...args),+ useVerifyProviderConnection: () => ({+ verifyProviderConnection: jest.fn(),+ }),+ };+});++jest.mock('@/components/confirm-delete-dialog', () => ({+ ConfirmDeleteDialog: ({ children }: { children: React.ReactNode }) =>+ children,+}));++jest.mock('@/components/ui/button', () => ({+ Button: ({+ children,+ ...props+ }: React.ButtonHTMLAttributes<HTMLButtonElement>) => (+ <button {...props}>{children}</button>+ ),+}));++jest.mock('@/components/originui/select-with-search', () => ({+ SelectWithSearch: () => null,+}));++jest.mock('@/components/ui/multi-select', () => ({ MultiSelect: () => null }));+jest.mock('@/components/ui/segmented', () => ({+ Segmented: ({ onChange }: { onChange: (value: string) => void }) => (+ <>+ <button type="button" onClick={() => onChange('access_key_secret')}>+ accessKey+ </button>+ <button type="button" onClick={() => onChange('bedrock_api_key')}>+ apiKey+ </button>+ </>+ ),+}));++jest.mock('./models-section', () => {+ const React = jest.requireActual<typeof import('react')>('react');+ return {+ ModelsSection: (props: {+ getFormValues: () => Record<string, unknown>;+ verifyTransform: (values: Record<string, unknown>) => unknown;+ onInstanceModelsChange?: (models: MockModelInfo[]) => void;+ onInstanceModelsEdited?: () => void;+ onInstanceModelsStatusChange?: (ready: boolean) => void;+ }) => {+ React.useEffect(() => {+ mockCatalogCredentials(props.verifyTransform(props.getFormValues()));+ props.onInstanceModelsChange?.(mockDiscoveredModels);+ if (mockInstanceModelsLoaded) {+ props.onInstanceModelsEdited?.();+ }+ // ModelsSection resolves credentials from its mount effect.+ // oxlint-disable-next-line react/exhaustive-deps+ }, []);+ React.useEffect(() => {+ props.onInstanceModelsStatusChange?.(mockInstanceModelsLoaded);+ });+ return null;+ },+ };+});+jest.mock('./verify-button', () => ({ __esModule: true, default: () => null }));++describe('BedrockInstanceCard', () => {+ beforeEach(() => {+ mockCatalogCredentials.mockReset();+ mockFetchProviderInstance+ .mockReset()+ .mockImplementation((providerName) => ({+ data: providerName ? savedInstanceDetails : undefined,+ refetch: mockRefetchProviderInstance,+ }));+ mockRefetchProviderInstance.mockReset();+ mockInstanceModelsLoaded = true;+ mockDiscoveredModels = [+ {+ model_name: 'amazon.nova-lite-v1:0',+ model_type: ['chat', 'vision'],+ max_tokens: 8192,+ },+ ];+ });++ it('creates an API key instance without manual model fields', async () => {+ const ref = createRef<TestCardRef>();+ render(+ <BedrockInstanceCard+ ref={ref}+ providerName="Bedrock"+ isDraft+ instance={{+ id: '',+ instance_name: '',+ provider_id: 'provider-id',+ region: 'us-east-1',+ status: 'active',+ api_key: '',+ }}+ />,+ );++ expect(screen.getByText('modelType')).toBeTruthy();+ expect(screen.getByPlaceholderText('bedrockModelNameMessage')).toBeTruthy();+ expect(screen.getByPlaceholderText('maxTokensTip')).toBeTruthy();++ fireEvent.click(screen.getByRole('button', { name: 'apiKey' }));++ expect(screen.queryByText('modelType')).toBeNull();+ expect(screen.queryByPlaceholderText('bedrockModelNameMessage')).toBeNull();+ expect(screen.queryByPlaceholderText('maxTokensTip')).toBeNull();++ fireEvent.change(screen.getByTestId('instance-name-input'), {+ target: { value: 'api-key-instance' },+ });+ fireEvent.change(screen.getByPlaceholderText('apiKeyMessage'), {+ target: { value: 'new-key' },+ });++ await waitFor(async () => expect(await ref.current?.validate()).toBe(true));++ const result = ref.current?.getSavePayload();+ expect(result).toMatchObject({+ apiKind: 'add',+ instanceName: 'api-key-instance',+ isDraft: true,+ payload: {+ instance_name: 'api-key-instance',+ llm_factory: 'Bedrock',+ api_key: {+ auth_mode: 'bedrock_api_key',+ bedrock_api_key: 'new-key',+ bedrock_region: 'us-east-1',+ },+ },+ });+ expect(result?.payload).toMatchObject({ model_info: mockDiscoveredModels });+ expect(result?.payload).not.toHaveProperty('max_tokens');+ });++ it('requires a discovered model before saving an API key instance', async () => {+ mockDiscoveredModels = [];+ const ref = createRef<TestCardRef>();+ render(+ <BedrockInstanceCard+ ref={ref}+ providerName="Bedrock"+ isDraft+ instance={{+ id: '',+ instance_name: '',+ provider_id: 'provider-id',+ region: 'us-east-1',+ status: 'active',+ api_key: '',+ }}+ />,+ );++ fireEvent.click(screen.getByRole('button', { name: 'apiKey' }));+ fireEvent.change(screen.getByTestId('instance-name-input'), {+ target: { value: 'api-key-instance' },+ });+ fireEvent.change(screen.getByPlaceholderText('apiKeyMessage'), {+ target: { value: 'new-key' },+ });++ await waitFor(async () =>+ expect(await ref.current?.validate()).toBe(false),+ );+ });++ it('updates saved credentials with the current model list', async () => {+ const ref = createRef<TestCardRef>();+ render(+ <BedrockInstanceCard+ ref={ref}+ providerName="Bedrock"+ defaultOpen+ instance={{+ id: 'instance-id',+ instance_name: 'saved-instance',+ provider_id: 'provider-id',+ region: 'us-east-1',+ status: 'active',+ api_key: '',+ }}+ />,+ );++ await screen.findByPlaceholderText('apiKeyMessage');+ await waitFor(() => expect(ref.current?.getSavePayload()).toBeNull());+ fireEvent.click(screen.getByRole('button', { name: 'accessKey' }));+ fireEvent.click(screen.getByRole('button', { name: 'apiKey' }));+ const input = await screen.findByPlaceholderText('apiKeyMessage');+ fireEvent.change(input, { target: { value: 'new-key' } });++ await waitFor(() => {+ const result = ref.current?.getSavePayload();+ expect(result).toMatchObject({+ apiKind: 'update',+ instanceName: 'saved-instance',+ isDraft: false,+ payload: {+ provider_name: 'Bedrock',+ id: 'instance-id',+ instance_name: 'saved-instance',+ verify: false,+ api_key: {+ auth_mode: 'bedrock_api_key',+ bedrock_api_key: 'new-key',+ bedrock_region: 'us-east-1',+ },+ model_info: mockDiscoveredModels,+ },+ });+ });+ });++ it('stops saving when the model snapshot becomes stale', async () => {+ const ref = createRef<TestCardRef>();+ const card = (+ <BedrockInstanceCard+ ref={ref}+ providerName="Bedrock"+ defaultOpen+ instance={{+ id: 'instance-id',+ instance_name: 'saved-instance',+ provider_id: 'provider-id',+ region: 'us-east-1',+ status: 'active',+ api_key: '',+ }}+ />+ );+ const { rerender } = render(card);++ await waitFor(async () => expect(await ref.current?.validate()).toBe(true));++ mockInstanceModelsLoaded = false;+ rerender(card);++ await waitFor(async () =>+ expect(await ref.current?.validate()).toBe(false),+ );+ });++ it('restores saved credentials before model discovery starts', async () => {+ mockFetchProviderInstance.mockReturnValue({+ data: undefined,+ refetch: mockRefetchProviderInstance,+ });+ const { rerender } = render(+ <BedrockInstanceCard+ providerName="Bedrock"+ defaultOpen+ instance={{+ id: 'instance-id',+ instance_name: 'saved-instance',+ provider_id: 'provider-id',+ region: 'us-east-1',+ status: 'active',+ api_key: '',+ }}+ />,+ );++ expect(mockCatalogCredentials).not.toHaveBeenCalled();++ mockFetchProviderInstance.mockReturnValue({+ data: savedInstanceDetails,+ refetch: mockRefetchProviderInstance,+ });+ rerender(+ <BedrockInstanceCard+ providerName="Bedrock"+ defaultOpen+ instance={{+ id: 'instance-id',+ instance_name: 'saved-instance',+ provider_id: 'provider-id',+ region: 'us-east-1',+ status: 'active',+ api_key: '',+ }}+ />,+ );++ await waitFor(() =>+ expect(mockCatalogCredentials).toHaveBeenCalledWith({+ apiKey: {+ auth_mode: 'bedrock_api_key',+ bedrock_api_key: 'old-key',+ bedrock_region: 'us-east-1',+ },+ baseUrl: undefined,+ region: 'default',+ }),+ );+ });+});diff --git a/web/src/pages/user-setting/setting-model/instance-card/bedrock-instance-card.tsx b/web/src/pages/user-setting/setting-model/instance-card/bedrock-instance-card.tsxindex 2d5686d0db..18f93a73fb 100644--- a/web/src/pages/user-setting/setting-model/instance-card/bedrock-instance-card.tsx+++ b/web/src/pages/user-setting/setting-model/instance-card/bedrock-instance-card.tsx@@ -26,6 +26,7 @@ import {import { Form } from '@/components/ui/form';import { Input } from '@/components/ui/input';import { MultiSelect } from '@/components/ui/multi-select';+import message from '@/components/ui/message';import { Segmented } from '@/components/ui/segmented';import { useTranslate } from '@/hooks/common-hooks';import { useBuildModelTypeOptions } from '@/hooks/logic-hooks/use-build-options';@@ -34,8 +35,8 @@ import {useFetchProviderInstance,useVerifyProviderConnection,} from '@/hooks/use-llm-request';-import { IProviderInstance } from '@/interfaces/database/llm';-import { IAddProviderInstanceRequestBody } from '@/interfaces/request/llm';+import type { IProviderInstance } from '@/interfaces/database/llm';+import type { IModelInfo } from '@/interfaces/request/llm';import { zodResolver } from '@hookform/resolvers/zod';import { ListChevronsDownUp, ListChevronsUpDown, Trash2 } from 'lucide-react';import {@@ -43,6 +44,7 @@ import {useCallback,useEffect,useImperativeHandle,+ useLayoutEffect,useMemo,useRef,useState,@@ -57,16 +59,22 @@ import {ProviderInstanceCardProps,ProviderInstanceCardRef,} from './interface';+import { unwrapApiKey } from './hooks';import { ModelsSection } from './models-section';import VerifyButton from './verify-button';-type AuthMode = 'access_key_secret' | 'iam_role' | 'assume_role';+type AuthMode =+ | 'access_key_secret'+ | 'iam_role'+ | 'assume_role'+ | 'bedrock_api_key';type BedrockFormValues = {auth_mode: AuthMode;bedrock_ak?: string;bedrock_sk?: string;aws_role_arn?: string;+ bedrock_api_key?: string;bedrock_region: string;llm_name: string;max_tokens: number;@@ -83,11 +91,11 @@ interface BedrockInstanceCardProps {/*** Inline instance card for AWS Bedrock. Renders Bedrock-specific fields- * (auth_mode segmented, ak/sk/arn, region, model name, max tokens,- * model_type) directly instead of going through the generic DynamicForm- * path. All fields are editable from the start (no name-first lock);- * the parent page's top Save button drives persistence through the- * imperative ref API.+ * (auth_mode segmented, credentials, region, and manual model settings)+ * directly instead of going through the generic DynamicForm path. API key+ * drafts discover models and persist them with the instance.+ * The parent page's top Save button drives persistence through the imperative+ * ref API.*/export const BedrockInstanceCard = forwardRef<ProviderInstanceCardRef,@@ -101,6 +109,8 @@ export const BedrockInstanceCard = forwardRef<const { buildModelTypeOptions } = useBuildModelTypeOptions();const [open, setOpen] = useState(isDraft || defaultOpen);const [draftName, setDraftName] = useState('');+ const modelInfoRef = useRef<IModelInfo[]>([]);+ const modelsLoadedRef = useRef(false);useEffect(() => {if (isDraft) {@@ -113,28 +123,51 @@ export const BedrockInstanceCard = forwardRef<z.object({auth_mode: z- .enum(['access_key_secret', 'iam_role', 'assume_role'])+ .enum([+ 'access_key_secret',+ 'iam_role',+ 'assume_role',+ 'bedrock_api_key',+ ]).default('access_key_secret'),bedrock_ak: z.string().optional(),bedrock_sk: z.string().optional(),aws_role_arn: z.string().optional(),+ bedrock_api_key: z.string().optional(),bedrock_region: z.string().min(1, { message: tSetting('bedrockRegionMessage') }),- llm_name: z- .string()- .min(1, { message: tSetting('bedrockModelNameMessage') }),- max_tokens: z- .number({- required_error: tSetting('maxTokensMessage'),- invalid_type_error: tSetting('maxTokensInvalidMessage'),- })- .nonnegative({ message: tSetting('maxTokensMinMessage') }),- model_type: z- .array(z.enum(['chat', 'embedding']))- .min(1, { message: tSetting('modelTypeMessage') }),+ llm_name: z.string(),+ max_tokens: z.number({+ required_error: tSetting('maxTokensMessage'),+ invalid_type_error: tSetting('maxTokensInvalidMessage'),+ }),+ model_type: z.array(z.enum(['chat', 'embedding'])),}).superRefine((data, ctx) => {+ if (isDraft && data.auth_mode !== 'bedrock_api_key') {+ if (!data.llm_name.trim()) {+ ctx.addIssue({+ code: z.ZodIssueCode.custom,+ message: tSetting('bedrockModelNameMessage'),+ path: ['llm_name'],+ });+ }+ if (data.model_type.length === 0) {+ ctx.addIssue({+ code: z.ZodIssueCode.custom,+ message: tSetting('modelTypeMessage'),+ path: ['model_type'],+ });+ }+ if (data.max_tokens < 0) {+ ctx.addIssue({+ code: z.ZodIssueCode.custom,+ message: tSetting('maxTokensMinMessage'),+ path: ['max_tokens'],+ });+ }+ }if (data.auth_mode === 'access_key_secret') {if (!data.bedrock_ak || !data.bedrock_ak.trim()) {ctx.addIssue({@@ -160,8 +193,18 @@ export const BedrockInstanceCard = forwardRef<});}}+ if (+ data.auth_mode === 'bedrock_api_key' &&+ !data.bedrock_api_key?.trim()+ ) {+ ctx.addIssue({+ code: z.ZodIssueCode.custom,+ message: tSetting('apiKeyMessage'),+ path: ['bedrock_api_key'],+ });+ }}),- [tSetting],+ [isDraft, tSetting],);const { data: instanceDetails, refetch: refetchInstanceDetails } =@@ -187,17 +230,16 @@ export const BedrockInstanceCard = forwardRef<const initialValues = useMemo<BedrockFormValues>(() => {const merged = { ...instance, ...(instanceDetails ?? {}) } as any;- const apiKey =- merged.api_key && typeof merged.api_key === 'object'- ? merged.api_key- : {};+ const { nested: apiKey } = unwrapApiKey(merged.api_key);return {auth_mode: (apiKey.auth_mode as AuthMode) ?? 'access_key_secret',bedrock_ak: apiKey.bedrock_ak ?? '',bedrock_sk: apiKey.bedrock_sk ?? '',aws_role_arn: apiKey.aws_role_arn ?? '',+ bedrock_api_key: apiKey.bedrock_api_key ?? '',bedrock_region:- merged.region && merged.region !== 'default' ? merged.region : '',+ apiKey.bedrock_region ??+ (merged.region && merged.region !== 'default' ? merged.region : ''),llm_name: '',max_tokens: 8192,model_type: ['chat'],@@ -209,9 +251,11 @@ export const BedrockInstanceCard = forwardRef<defaultValues: initialValues,});- useEffect(() => {+ useLayoutEffect(() => {// Reset form when initial values change (e.g. instance details load).- form.reset(initialValues);+ // ModelsSection reads the form in a passive effect, so credentials must+ // be restored before child effects start model discovery.+ form.reset(initialValues, { keepDirtyValues: true });// oxlint-disable-next-line react/exhaustive-deps}, [initialValues]);@@ -224,12 +268,13 @@ export const BedrockInstanceCard = forwardRef<// Build a Bedrock-shaped payload for both submit and verify flows.const buildPayload = useCallback(- (values: BedrockFormValues, instanceName: string) => {+ (values: BedrockFormValues, instanceName: string, forDraft: boolean) => {const cleaned: Record<string, any> = { ...values };const fieldsByMode: Record<AuthMode, string[]> = {access_key_secret: ['bedrock_ak', 'bedrock_sk'],iam_role: ['aws_role_arn'],assume_role: [],+ bedrock_api_key: ['bedrock_api_key'],};(Object.keys(fieldsByMode) as AuthMode[]).forEach((mode) => {if (mode !== values.auth_mode) {@@ -247,60 +292,89 @@ export const BedrockInstanceCard = forwardRef<model_type: values.model_type,};const { instancePayload, modelPayload } = splitProviderPayload(flat);- return {- ...instancePayload,- max_tokens: modelPayload.max_tokens,- model_info: [modelPayload],- } as IAddProviderInstanceRequestBody;+ const payload: Record<string, any> = {+ instance_name: instanceName,+ api_key: instancePayload.api_key,+ base_url: instancePayload.base_url,+ region: instancePayload.region,+ };+ if (forDraft) {+ payload.llm_factory = providerName;+ }+ if (forDraft && values.auth_mode !== 'bedrock_api_key') {+ payload.max_tokens = modelPayload.max_tokens;+ payload.model_info = [modelPayload];+ } else {+ payload.model_info = modelInfoRef.current;+ }+ return payload;},[providerName],);- const { verifyProviderConnection } = useVerifyProviderConnection();- const handleVerify = useCallback(- async (params: any) => {- const isValid = await form.trigger();- if (!isValid) {- return {- isValid: false,- logs: tSetting('bedrockRegionMessage'),- } as VerifyResult;- }- const values = form.getValues();+ const transformModelCredentials = useCallback(+ (values: Record<string, any>) => {const payload = buildPayload(- values,+ values as BedrockFormValues,draftName.trim() || instance.instance_name,+ false,);- const { instancePayload, modelPayload } = splitProviderPayload({- ...payload,- ...values,- llm_factory: providerName,- instance_name: draftName.trim() || instance.instance_name,- });- const ret = await verifyProviderConnection({- provider_name: providerName,- api_key: JSON.stringify(instancePayload.api_key),- base_url: instancePayload.base_url,- region: instancePayload.region,- model_info: [modelPayload],- ...params,- });return {- isValid: ret.code === 0,- logs: ret.message,- } as VerifyResult;+ apiKey: payload.api_key,+ baseUrl: payload.base_url,+ region: payload.region,+ };},- [- form,- providerName,- buildPayload,- draftName,- instance.instance_name,- verifyProviderConnection,- tSetting,- ],+ [buildPayload, draftName, instance.instance_name],);+ const getModelFormValues = useCallback(() => {+ const values = form.getValues();+ const credentials = transformModelCredentials(values);+ return {+ ...values,+ api_key: credentials.apiKey,+ base_url: credentials.baseUrl,+ };+ }, [form, transformModelCredentials]);++ const { verifyProviderConnection } = useVerifyProviderConnection();+ const handleVerify = useCallback(async () => {+ const isValid = await form.trigger();+ if (!isValid) {+ return {+ isValid: false,+ logs: tSetting('bedrockRegionMessage'),+ } as VerifyResult;+ }+ const values = form.getValues();+ const payload = buildPayload(+ values,+ draftName.trim() || instance.instance_name,+ isDraft,+ );+ const ret = await verifyProviderConnection({+ provider_name: providerName,+ api_key: JSON.stringify(payload.api_key),+ base_url: payload.base_url,+ region: payload.region,+ model_info: payload.model_info,+ });+ return {+ isValid: ret.code === 0,+ logs: ret.message,+ } as VerifyResult;+ }, [+ form,+ providerName,+ buildPayload,+ draftName,+ instance.instance_name,+ verifyProviderConnection,+ tSetting,+ isDraft,+ ]);+const { deleteProviderInstance } = useDeleteProviderInstance();const handleDelete = useCallback(async () => {if (isDraft) {@@ -336,16 +410,23 @@ export const BedrockInstanceCard = forwardRef<}if (!instanceDetails && !instance.id) return;const baselineValues = initialValues;- const baseline = buildPayload(baselineValues, instance.instance_name);+ const baseline = buildPayload(+ baselineValues,+ instance.instance_name,+ false,+ );const finalBaseline = {...baseline,+ provider_name: providerName,id: instanceDetails?.id || instance.id,+ verify: false,};baselinePayloadRef.current = JSON.stringify(finalBaseline);}, [isDraft,initialValues,buildPayload,+ providerName,instance.instance_name,instance.id,instanceDetails,@@ -356,7 +437,7 @@ export const BedrockInstanceCard = forwardRef<if (isDraft) {if (!trimmed) return null;const values = form.getValues();- const payload = buildPayload(values, trimmed);+ const payload = buildPayload(values, trimmed, true);return {payload,instanceName: trimmed,@@ -365,17 +446,13 @@ export const BedrockInstanceCard = forwardRef<apiKind: 'add' as const,};}- // Skip cards the user hasn't actually edited. Collapsed cards- // never fetch `instanceDetails`, so their form is still on empty- // defaults - the signature check below would false-positive on- // defaulting differences. `form.formState.isDirty` tracks real- // user interaction.- if (!form.formState.isDirty) return null;const values = form.getValues();- const payload = buildPayload(values, instance.instance_name);+ const payload = buildPayload(values, instance.instance_name, false);const finalPayload = {...payload,+ provider_name: providerName,id: instanceDetails?.id || instance.id,+ verify: false,};const sig = JSON.stringify(finalPayload);if (sig === baselinePayloadRef.current) return null;@@ -383,14 +460,13 @@ export const BedrockInstanceCard = forwardRef<payload: finalPayload,instanceName: instance.instance_name,isDraft: false,- // Bedrock saved cards update via `addProviderInstance` with an `id`- // (matches the legacy auto-save behaviour).- apiKind: 'add' as const,+ apiKind: 'update' as const,};}, [isDraft,form,buildPayload,+ providerName,instance.instance_name,instance.id,instanceDetails,@@ -403,44 +479,78 @@ export const BedrockInstanceCard = forwardRef<}}, [getSavePayload]);+ const markModelsEdited = useCallback(() => {+ if (isDraft || !baselinePayloadRef.current) return;+ try {+ const baseline = JSON.parse(baselinePayloadRef.current) as Record<+ string,+ any+ >;+ baseline.model_info = modelInfoRef.current;+ baselinePayloadRef.current = JSON.stringify(baseline);+ } catch {+ // ignore parse errors - baseline will be re-seeded on details reload+ }+ }, [isDraft]);+useImperativeHandle(ref,() => ({validate: async () => {if (isDraft && !draftNameRef.current.trim()) return false;+ if (!isDraft && !modelsLoadedRef.current) {+ message.error(tSetting('selectModelBeforeSave'));+ return false;+ }+ if (+ isDraft &&+ authMode === 'bedrock_api_key' &&+ modelInfoRef.current.length === 0+ ) {+ message.error(tSetting('selectModelBeforeSave'));+ return false;+ }const isValid = await form.trigger();return !!isValid;},getSavePayload,markSaved,}),- [isDraft, form, getSavePayload, markSaved],+ [isDraft, authMode, form, getSavePayload, markSaved, tSetting],);// ──────────────── Field group rendered in both modes ────────────────const renderFields = () => (<Form {...form}><form className="space-y-6" onSubmit={(e) => e.preventDefault()}>- <RAGFlowFormItem- name="model_type"- label={tSetting('modelType')}- required- >- {(field) => (- <MultiSelect- options={buildModelTypeOptions(['chat', 'embedding'])}- placeholder={tSetting('modelTypeMessage')}- onValueChange={field.onChange}- defaultValue={field.value}- variant="inverted"- maxCount={100}- />- )}- </RAGFlowFormItem>+ {isDraft && authMode !== 'bedrock_api_key' && (+ <>+ <RAGFlowFormItem+ name="model_type"+ label={tSetting('modelType')}+ required+ >+ {(field) => (+ <MultiSelect+ options={buildModelTypeOptions(['chat', 'embedding'])}+ placeholder={tSetting('modelTypeMessage')}+ onValueChange={field.onChange}+ defaultValue={field.value}+ variant="inverted"+ maxCount={100}+ />+ )}+ </RAGFlowFormItem>- <RAGFlowFormItem name="llm_name" label={tSetting('modelName')} required>- <Input placeholder={tSetting('bedrockModelNameMessage')} />- </RAGFlowFormItem>+ <RAGFlowFormItem+ name="llm_name"+ label={tSetting('modelName')}+ required+ >+ <Input placeholder={tSetting('bedrockModelNameMessage')} />+ </RAGFlowFormItem>+ </>+ )}<div><RAGFlowFormItem name="auth_mode">@@ -448,6 +558,9 @@ export const BedrockInstanceCard = forwardRef<<Segmentedvalue={field.value}onChange={(value) => {+ if (isDraft && value !== field.value) {+ modelInfoRef.current = [];+ }if (value !== 'access_key_secret') {form.setValue('bedrock_ak', '');form.setValue('bedrock_sk', '');@@ -455,6 +568,9 @@ export const BedrockInstanceCard = forwardRef<if (value !== 'iam_role') {form.setValue('aws_role_arn', '');}+ if (value !== 'bedrock_api_key') {+ form.setValue('bedrock_api_key', '');+ }field.onChange(value);}}options={[@@ -467,6 +583,10 @@ export const BedrockInstanceCard = forwardRef<label: tSetting('awsAuthModeAssumeRole'),value: 'assume_role',},+ {+ label: tSetting('apiKey'),+ value: 'bedrock_api_key',+ },]}/>)}@@ -502,6 +622,20 @@ export const BedrockInstanceCard = forwardRef<</RAGFlowFormItem>)}+ {authMode === 'bedrock_api_key' && (+ <RAGFlowFormItem+ name="bedrock_api_key"+ label={tSetting('apiKey')}+ required+ >+ <Input+ type="password"+ placeholder={tSetting('apiKeyMessage')}+ autoComplete="off"+ />+ </RAGFlowFormItem>+ )}+{authMode === 'assume_role' && (<div className="text-sm text-text-secondary">{tSetting('awsAssumeRoleTip')}@@ -524,28 +658,32 @@ export const BedrockInstanceCard = forwardRef<)}</RAGFlowFormItem>- <RAGFlowFormItem- name="max_tokens"- label={tSetting('maxTokens')}- required- >- {(field) => (- <Input- type="number"- placeholder={tSetting('maxTokensTip')}- value={field.value}- onChange={(e) => field.onChange(Number(e.target.value))}- />- )}- </RAGFlowFormItem>+ {isDraft && authMode !== 'bedrock_api_key' && (+ <RAGFlowFormItem+ name="max_tokens"+ label={tSetting('maxTokens')}+ required+ >+ {(field) => (+ <Input+ type="number"+ placeholder={tSetting('maxTokensTip')}+ value={field.value}+ onChange={(e) => field.onChange(Number(e.target.value))}+ />+ )}+ </RAGFlowFormItem>+ )}</form>{/* VerifyButton lives inside <Form> (FormProvider) so itsinternal useFormContext() resolves the form instance.Rendered outside <form> so it never triggers submission. */}- <div className="pt-3">- <VerifyButton onVerify={handleVerify} isAbsolute={false} />- </div>+ {authMode !== 'bedrock_api_key' && (+ <div className="pt-3">+ <VerifyButton onVerify={handleVerify} isAbsolute={false} />+ </div>+ )}</Form>);@@ -594,12 +732,17 @@ export const BedrockInstanceCard = forwardRef<<div className="pt-3"><ModelsSection+ key={authMode}providerName={providerName}instanceName={instance.instance_name || '__draft__'}instance={instance}hideActions={false}hideIfEmpty={false}- getFormValues={() => form.getValues()}+ getFormValues={getModelFormValues}+ verifyTransform={transformModelCredentials}+ onInstanceModelsChange={(info) => {+ modelInfoRef.current = info;+ }}/></div></div>@@ -652,16 +795,27 @@ export const BedrockInstanceCard = forwardRef<<div className="px-2 pb-4 flex flex-col gap-4">{renderFields()}- <div className="pt-3">- <ModelsSection- providerName={providerName}- instanceName={instance.instance_name || '__draft__'}- instance={instance}- hideActions={false}- hideIfEmpty={false}- getFormValues={() => form.getValues()}- />- </div>+ {instanceDetails && (+ <div className="pt-3">+ <ModelsSection+ providerName={providerName}+ instanceName={instance.instance_name || '__draft__'}+ instance={instance}+ hideActions={false}+ hideIfEmpty={false}+ instanceDetailsLoaded+ getFormValues={getModelFormValues}+ verifyTransform={transformModelCredentials}+ onInstanceModelsChange={(info) => {+ modelInfoRef.current = info;+ }}+ onInstanceModelsEdited={markModelsEdited}+ onInstanceModelsStatusChange={(ready) => {+ modelsLoadedRef.current = ready;+ }}+ />+ </div>+ )}</div></CollapsibleContent></Collapsible>diff --git a/web/src/pages/user-setting/setting-model/instance-card/components/saved-mode-card.tsx b/web/src/pages/user-setting/setting-model/instance-card/components/saved-mode-card.tsxindex 93a4102e1d..119bdf9f91 100644--- a/web/src/pages/user-setting/setting-model/instance-card/components/saved-mode-card.tsx+++ b/web/src/pages/user-setting/setting-model/instance-card/components/saved-mode-card.tsx@@ -64,6 +64,7 @@ export function SavedModeCard({instance,instanceDetailsLoaded,modelInfoRef,+ onInstanceModelsStatusChange,draftName,open,setOpen,@@ -235,6 +236,7 @@ export function SavedModeCard({modelInfoRef.current = info;}}onInstanceModelsEdited={handleInstanceModelsEdited}+ onInstanceModelsStatusChange={onInstanceModelsStatusChange}/></div>)}diff --git a/web/src/pages/user-setting/setting-model/instance-card/hooks.tsx b/web/src/pages/user-setting/setting-model/instance-card/hooks.tsxindex 1217288e13..6db130e201 100644--- a/web/src/pages/user-setting/setting-model/instance-card/hooks.tsx+++ b/web/src/pages/user-setting/setting-model/instance-card/hooks.tsx@@ -63,8 +63,8 @@ export function buildApiKeyValue(* 1. a raw JSON string `'{"api_key":"sk-x","group_id":"123"}'`* 2. an already-parsed object `{ api_key, group_id }`* 3. a plain bare key string `'sk-x'`- * Normalise all three into the bare key plus any nested credential- * fields so the form pre-fills group_id / api_version / provider_order.+ * Normalise all three into the bare key plus the registered nested+ * credential fields so provider forms can restore their inputs.*/export function unwrapApiKey(raw: unknown): {apiKey: string;@@ -165,8 +165,8 @@ export function useProviderBaseUrlOptions(providerName: string) {* nested `spark_api_password`), use it to reverse the corresponding* `submitTransform` so the provider-specific credential fields* pre-fill. Otherwise fall back to the generic `unwrapApiKey` path- * which lifts the bare `api_key` plus `API_KEY_NESTED_FIELDS`- * (`group_id` / `api_version` / `provider_order`).+ * which lifts the bare `api_key` plus the provider-specific fields+ * registered in `API_KEY_NESTED_FIELDS`.*/export function useProviderInitialValues(instance: IProviderInstance,@@ -203,9 +203,8 @@ export function useProviderInitialValues(} else if (merged.api_key) {// api_key may come back as a JSON string, an already-parsed object,// or a plain bare key (see `unwrapApiKey`). Normalise it so the- // api_key text field shows the bare key and the nested credential- // fields (MiniMax group_id, Azure api_version, OpenRouter- // provider_order) pre-fill their own form inputs.+ // api_key text field shows the bare key and registered nested+ // credentials pre-fill their own form inputs.const { apiKey, nested } = unwrapApiKey(merged.api_key);values.api_key = apiKey;Object.assign(values, nested);@@ -460,9 +459,9 @@ interface UseInstanceSaveStateArgs {* - `getSavePayload()`: return the body if dirty (or a draft with a* name), else `null` so the parent can skip the redundant call.* - `markSaved()`: re-baseline after a successful save.- * - `markModelsEdited()`: absorb a model PATCH into the baseline so- * the next top-save does not re-PUT the same model_info (the PATCH- * endpoint already persisted it).+ * - `markModelsEdited()`: absorb the authoritative saved model snapshot+ * into the baseline so the next top-save does not re-PUT model_info+ * that the backend already persisted.** The dirty check compares a JSON signature of the current payload to* the baseline signature, mirroring the old `lastSavedPayloadRef`@@ -682,15 +681,11 @@ export function useInstanceSaveState({}}, [buildPayload]);- // Absorb a model patch into the baseline. `patchInstanceModel` has- // already persisted the new max_tokens / model_type / features- // server-side, so the next top-save should NOT re-PUT the same- // model_info. By parsing the previously-saved baseline and overwriting- // ONLY model_info, the baseline now matches the current state and the- // signature check in `getSavePayload` short-circuits - while any- // in-flight edits to api_key / base_url / region remain in the- // baseline unchanged and will still trigger a save via signature- // mismatch.+ // Absorb the latest backend-fetched model list into the baseline. By+ // parsing the previously-saved baseline and overwriting ONLY model_info,+ // the signature check in `getSavePayload` short-circuits for an unchanged+ // card while any in-flight api_key / base_url / region edits still differ+ // from the baseline and remain eligible for saving.//// Skipped for drafts (the baseline is empty there) and until the// baseline has been seeded.diff --git a/web/src/pages/user-setting/setting-model/instance-card/interface.ts b/web/src/pages/user-setting/setting-model/instance-card/interface.tsindex 87155a81f3..429947d9bd 100644--- a/web/src/pages/user-setting/setting-model/instance-card/interface.ts+++ b/web/src/pages/user-setting/setting-model/instance-card/interface.ts@@ -76,11 +76,8 @@ export interface InstanceSavePayload {isDraft: boolean;/*** Which save endpoint the parent should dispatch to:- * - `'add'`: call `addProviderInstance` (drafts of any provider, plus- * Bedrock saved cards which carry an `id` inside the- * `addProviderInstance` body).- * - `'update'`: call `updateProviderInstance` (generic saved cards,- * whose payload matches `IUpdateProviderInstanceRequestBody`).+ * - `'add'`: call `addProviderInstance` for drafts.+ * - `'update'`: call `updateProviderInstance` for saved cards.*/apiKind: 'add' | 'update';}@@ -119,16 +116,23 @@ export interface ProviderInstanceCardProps {/*** Provider-specific credential fields that the backend expects bundled* *inside* `api_key` as an object rather than as top-level keys:- * api_key: { api_key, group_id?, api_version?, provider_order? }+ * api_key: { api_key?, ...providerCredentials }* - MiniMax → group_id* - Azure OpenAI → api_version* - OpenRouter → provider_order+ * - Bedrock → auth_mode / AWS credentials / region* When none of these are present the api_key stays a bare string.*/export const API_KEY_NESTED_FIELDS = ['group_id','api_version','provider_order',+ 'auth_mode',+ 'bedrock_ak',+ 'bedrock_sk',+ 'aws_role_arn',+ 'bedrock_api_key',+ 'bedrock_region',] as const;export type ApiKeyNestedField = (typeof API_KEY_NESTED_FIELDS)[number];@@ -162,6 +166,7 @@ export interface SavedModeCardProps {instance: IProviderInstance;instanceDetailsLoaded: boolean;modelInfoRef: React.MutableRefObject<IModelInfo[]>;+ onInstanceModelsStatusChange: (ready: boolean) => void;draftName: string;open: boolean;setOpen: (open: boolean) => void;diff --git a/web/src/pages/user-setting/setting-model/instance-card/models-section/hooks.test.tsx b/web/src/pages/user-setting/setting-model/instance-card/models-section/hooks.test.tsxnew file mode 100644index 0000000000..15f7b862c6--- /dev/null+++ b/web/src/pages/user-setting/setting-model/instance-card/models-section/hooks.test.tsx@@ -0,0 +1,431 @@+/*+ * Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { QueryClient, QueryClientProvider } from '@tanstack/react-query';+import { act, renderHook, waitFor } from '@testing-library/react';+import { useRef } from 'react';++import { useInstanceSaveState } from '../hooks';+import {+ DRAFT_INSTANCE_SENTINEL,+ useModelMutations,+ useModelsCatalog,+ useModelsDerived,+ useModelEdit,+ useModelVerify,+ useResolveCreds,+} from './hooks';++const mockListProviderModels = jest.fn();+const mockUpdateProviderInstance = jest.fn();+const mockVerifyProviderConnection = jest.fn();+const mockPatchInstanceModel = jest.fn();+function mockInstanceModelsKey(providerName: string, instanceName: string) {+ return ['addedProviders', providerName, instanceName, 'models'];+}++jest.mock('@/hooks/use-llm-request', () => ({+ LlmKeys: { instanceModels: mockInstanceModelsKey },+ useAddInstanceModel: () => ({ addInstanceModel: jest.fn() }),+ useDeleteInstanceModels: () => ({ deleteInstanceModels: jest.fn() }),+ useListProviderModels: () => ({+ listProviderModels: mockListProviderModels,+ }),+ usePatchInstanceModel: () => ({+ patchInstanceModel: mockPatchInstanceModel,+ loading: false,+ }),+ useUpdateProviderInstance: () => ({+ updateProviderInstance: mockUpdateProviderInstance,+ loading: false,+ }),+ useVerifyProviderConnection: () => ({+ verifyProviderConnection: mockVerifyProviderConnection,+ }),+}));++jest.mock('@/components/dynamic-form', () => ({}));++jest.mock('../../provider-schema/hooks', () => ({+ useProviderFields: jest.fn(),+}));++jest.mock('../use-custom-model-fields', () => ({+ useCustomModelFields: () => [],+}));++jest.mock('@/services/llm-service', () => ({+ __esModule: true,+ default: { verifyProviderConnection: jest.fn() },+}));++jest.mock('../available-models', () => ({+ sortModelTypes: (types: string[]) => types,+}));++describe('Bedrock model credentials', () => {+ beforeEach(() => {+ mockListProviderModels.mockReset().mockResolvedValue({ code: 0, data: [] });+ mockUpdateProviderInstance.mockReset().mockResolvedValue(undefined);+ mockVerifyProviderConnection.mockReset().mockResolvedValue({ code: 0 });+ });++ it('waits for saved Bedrock API key details before listing models', async () => {+ let apiKey: Record<string, string> = {+ auth_mode: 'access_key_secret',+ bedrock_api_key: '',+ bedrock_region: '',+ };+ const resolveCreds = () => ({ apiKey, baseUrl: undefined });++ const { rerender } = renderHook(+ ({ instanceDetailsLoaded }) =>+ useModelsCatalog({+ providerName: 'Bedrock',+ instanceName: 'bedrock-instance',+ hideActions: false,+ resolveCreds,+ instanceModels: undefined,+ apiKeyValue: apiKey,+ baseUrlValue: undefined,+ instanceDetailsLoaded,+ }),+ { initialProps: { instanceDetailsLoaded: false } },+ );++ expect(mockListProviderModels).not.toHaveBeenCalled();++ apiKey = {+ auth_mode: 'bedrock_api_key',+ bedrock_api_key: 'saved-key',+ bedrock_region: 'us-east-1',+ };+ rerender({ instanceDetailsLoaded: true });++ await waitFor(() =>+ expect(mockListProviderModels).toHaveBeenCalledWith({+ provider_name: 'Bedrock',+ api_key: apiKey,+ base_url: undefined,+ }),+ );+ });++ it('lists models explicitly for a Bedrock API key draft', async () => {+ const apiKey = {+ auth_mode: 'bedrock_api_key',+ bedrock_api_key: 'draft-key',+ bedrock_region: 'us-east-1',+ };+ const resolveCreds = () => ({ apiKey, baseUrl: undefined });++ const { result } = renderHook(() =>+ useModelsCatalog({+ providerName: 'Bedrock',+ instanceName: DRAFT_INSTANCE_SENTINEL,+ hideActions: false,+ resolveCreds,+ instanceModels: undefined,+ apiKeyValue: apiKey,+ baseUrlValue: undefined,+ }),+ );++ expect(mockListProviderModels).not.toHaveBeenCalled();++ await act(async () => result.current.handleListModels());++ expect(mockListProviderModels).toHaveBeenCalledWith({+ provider_name: 'Bedrock',+ api_key: apiKey,+ base_url: undefined,+ });+ });++ it('preserves structured credentials for verify and batch update', async () => {+ const apiKey = {+ auth_mode: 'bedrock_api_key',+ bedrock_api_key: 'saved-key',+ bedrock_region: 'us-east-1',+ };+ const model = {+ name: 'anthropic.claude',+ model_types: ['chat'],+ max_tokens: 8192,+ features: [],+ };+ const instance = {+ id: 'instance-id',+ instance_name: 'bedrock-instance',+ provider_id: 'provider-id',+ region: 'default',+ status: 'active',+ api_key: '',+ };++ const { result } = renderHook(() => {+ const { resolveCreds } = useResolveCreds(instance, () => ({+ api_key: apiKey,+ }));+ const verify = useModelVerify({+ providerName: 'Bedrock',+ resolveCreds,+ instanceModels: [],+ instance,+ getFormValues: () => ({ auth_mode: 'bedrock_api_key' }),+ verifyTransform: () => ({ apiKey, region: 'default' }),+ });+ const mutations = useModelMutations({+ providerName: 'Bedrock',+ instanceName: instance.instance_name,+ isDraftInstance: false,+ hideActions: false,+ resolveCreds,+ instance,+ instanceItems: [],+ filteredModels: [model],+ addedSet: new Set(),+ setCatalog: jest.fn(),+ clearCatalogOverride: jest.fn(),+ });+ return { verify, mutations };+ });++ await act(async () => result.current.verify.handleVerify(model));+ expect(mockVerifyProviderConnection).toHaveBeenCalledWith(+ expect.objectContaining({ api_key: apiKey, region: 'default' }),+ );++ await act(async () => result.current.mutations.handleBatchToggleModels());+ expect(mockUpdateProviderInstance).toHaveBeenCalledWith(+ expect.objectContaining({ api_key: apiKey, region: 'default' }),+ );+ });+});++describe('saved instance model baseline', () => {+ it('does not accept a failed model query as an authoritative snapshot', () => {+ const onInstanceModelsEdited = jest.fn();++ renderHook(() =>+ useModelsDerived({+ catalog: [],+ instanceModels: [],+ instanceModelsLoading: false,+ instanceModelsSucceeded: false,+ draftModels: [],+ isDraftInstance: false,+ onInstanceModelsChange: jest.fn(),+ onInstanceModelsEdited,+ }),+ );++ expect(onInstanceModelsEdited).not.toHaveBeenCalled();+ });++ it('stays clean after persisted models load without hiding credential edits', async () => {+ const initialValues = {+ api_key: 'saved-key',+ base_url: '',+ region: 'default',+ };+ const instanceDetails = {+ id: 'instance-id',+ instance_name: 'saved-instance',+ provider_id: 'provider-id',+ region: 'default',+ status: 'active',+ api_key: 'saved-key',+ };+ const persistedModel = {+ name: 'saved-model',+ model_type: ['chat'],+ max_tokens: 8192,+ status: 'active',+ };+ const formValues = { ...initialValues };++ const { result, rerender } = renderHook(+ ({ instanceModels, instanceModelsLoading, instanceModelsSucceeded }) => {+ const modelInfoRef = useRef<+ {+ model_name: string;+ model_type: string | string[];+ max_tokens: number;+ extra?: Record<string, unknown>;+ }[]+ >([]);+ const formRef = useRef({+ submit: jest.fn(),+ isDirty: () => false,+ getValues: () => formValues,+ getFilteredValues: () => formValues,+ reset: jest.fn(),+ trigger: jest.fn(),+ watch: jest.fn(),+ watchDirty: jest.fn(),+ updateFieldType: jest.fn(),+ onFieldUpdate: jest.fn(),+ });+ const saveState = useInstanceSaveState({+ formRef,+ providerName: 'Bedrock',+ instanceName: instanceDetails.instance_name,+ instanceId: instanceDetails.id,+ isDraft: false,+ draftName: '',+ instanceDetails,+ initialValues,+ modelInfoRef,+ });++ useModelsDerived({+ catalog: [],+ instanceModels,+ instanceModelsLoading,+ instanceModelsSucceeded,+ draftModels: [],+ isDraftInstance: false,+ onInstanceModelsChange: (modelInfo) => {+ modelInfoRef.current = modelInfo;+ },+ onInstanceModelsEdited: saveState.markModelsEdited,+ });++ return saveState;+ },+ {+ initialProps: {+ instanceModels: [] as (typeof persistedModel)[],+ instanceModelsLoading: true,+ instanceModelsSucceeded: false,+ },+ },+ );++ formValues.api_key = 'changed-before-models-load';+ expect(result.current.getSavePayload()?.payload.api_key).toBe(+ 'changed-before-models-load',+ );+ formValues.api_key = 'saved-key';++ rerender({+ instanceModels: [persistedModel],+ instanceModelsLoading: false,+ instanceModelsSucceeded: true,+ });++ await waitFor(() => expect(result.current.getSavePayload()).toBeNull());++ formValues.api_key = 'changed-key';+ expect(result.current.getSavePayload()?.payload.api_key).toBe(+ 'changed-key',+ );+ });++ it('updates the saved snapshot only after a model edit succeeds', async () => {+ const providerName = 'Bedrock';+ const instanceName = 'saved-instance';+ const persistedModel = {+ name: 'saved-model',+ model_type: ['chat'],+ max_tokens: 4096,+ status: 'active',+ };+ const queryClient = new QueryClient();+ const queryKey = mockInstanceModelsKey(providerName, instanceName);+ queryClient.setQueryData(queryKey, [persistedModel]);+ let resolvePatch!: (value: { code: number }) => void;+ mockPatchInstanceModel.mockReset().mockImplementationOnce(+ () =>+ new Promise<{ code: number }>((resolve) => {+ resolvePatch = resolve;+ }),+ );++ const wrapper = ({ children }: { children: React.ReactNode }) => (+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>+ );+ const { result } = renderHook(+ () =>+ useModelEdit({+ providerName,+ instanceName,+ addedSet: new Set([persistedModel.name]),+ updateCatalogModel: jest.fn(),+ clearCatalogOverride: jest.fn(),+ }),+ { wrapper },+ );++ act(() => {+ result.current.setEditingModel({+ name: persistedModel.name,+ model_types: persistedModel.model_type,+ max_tokens: persistedModel.max_tokens,+ features: [],+ });+ });+ let submitPromise!: Promise<void>;+ act(() => {+ submitPromise = result.current.handleEditSubmit({+ name: persistedModel.name,+ model_types: persistedModel.model_type,+ max_tokens: 8192,+ features: [],+ });+ });++ expect(queryClient.getQueryData(queryKey)).toEqual([persistedModel]);++ await act(async () => {+ resolvePatch({ code: 0 });+ await submitPromise;+ });++ expect(mockPatchInstanceModel).toHaveBeenCalledWith(+ expect.objectContaining({+ model_name: persistedModel.name,+ max_tokens: 8192,+ }),+ );+ expect(queryClient.getQueryData(queryKey)).toEqual([+ expect.objectContaining({ max_tokens: 8192 }),+ ]);++ mockPatchInstanceModel.mockResolvedValueOnce({ code: 1 });+ act(() => {+ result.current.setEditingModel({+ name: persistedModel.name,+ model_types: persistedModel.model_type,+ max_tokens: 8192,+ features: [],+ });+ });+ await act(async () => {+ await result.current.handleEditSubmit({+ name: persistedModel.name,+ model_types: persistedModel.model_type,+ max_tokens: 16384,+ features: [],+ });+ });++ expect(queryClient.getQueryData(queryKey)).toEqual([+ expect.objectContaining({ max_tokens: 8192 }),+ ]);+ });+});diff --git a/web/src/pages/user-setting/setting-model/instance-card/models-section/hooks.ts b/web/src/pages/user-setting/setting-model/instance-card/models-section/hooks.tsindex 62069d6bea..a89de1315e 100644--- a/web/src/pages/user-setting/setting-model/instance-card/models-section/hooks.ts+++ b/web/src/pages/user-setting/setting-model/instance-card/models-section/hooks.ts@@ -91,7 +91,10 @@ export const buildModelInfo = (items: IProviderModelItem[]): IModelInfo[] =>* field (e.g. VolcEngine, Google Cloud) so the auto-fetch gate can* distinguish "no base_url field" from "base_url field exists but is* empty". */-export type ResolvedCreds = { apiKey: string; baseUrl: string | undefined };+export type ResolvedCreds = {+ apiKey: string | object;+ baseUrl: string | undefined;+};// ---------------------------------------------------------------------------// 1. useResolveCreds — resolve api_key / base_url from host form or instance@@ -107,7 +110,7 @@ export function useResolveCreds(const resolveCreds = useCallback((): ResolvedCreds => {const fv = getFormValues?.() ?? {};return {- apiKey: (fv.api_key as string) ?? instance?.api_key ?? '',+ apiKey: (fv.api_key as string | object) ?? instance?.api_key ?? '',baseUrl: (fv.base_url as string) ?? instance?.base_url,};}, [getFormValues, instance]);@@ -126,7 +129,7 @@ interface UseModelsCatalogArgs {resolveCreds: () => ResolvedCreds;instanceModels: IInstanceModel[] | undefined;- apiKeyValue: string;+ apiKeyValue: ResolvedCreds['apiKey'];baseUrlValue: string | undefined;@@ -236,25 +239,42 @@ export function useModelsCatalog({// to know which server to query. For every other provider we fetch on// mount regardless.//- // The credential check is performed INSIDE the effect (not in the deps)- // because for saved cards the form is reset by- // `useFormResetOnDetailsLoad` in a parent effect that runs BEFORE this- // one. Reading `resolveCreds()` at effect time picks up the freshly- // reset `base_url` / `api_key` values, whereas reading them during- // render would see the stale (pre-reset) values and defer forever.-- const requiresApiKey = providerName === LLMFactory.VolcEngine;+ // The credential check is performed INSIDE the effect (not in the deps).+ // The host restores saved credentials before passive effects run, so this+ // reads the current form snapshot instead of the previous render's values.const hasAutoFetchedRef = useRef(false);useEffect(() => {if (hasAutoFetchedRef.current) return;if (hideActions) return;if (!providerName) return;+ if (+ providerName === LLMFactory.Bedrock &&+ instanceName === DRAFT_INSTANCE_SENTINEL+ )+ return;const creds = resolveCreds();+ const apiKeyConfig =+ typeof creds.apiKey === 'object' && creds.apiKey !== null+ ? (creds.apiKey as Record<string, unknown>)+ : undefined;+ const requiresApiKey =+ providerName === LLMFactory.VolcEngine ||+ (providerName === LLMFactory.Bedrock &&+ apiKeyConfig?.auth_mode === 'bedrock_api_key');+ const hasApiKey =+ providerName === LLMFactory.Bedrock && requiresApiKey+ ? Boolean(apiKeyConfig?.bedrock_api_key && apiKeyConfig?.bedrock_region)+ : Boolean(creds.apiKey);+ const waitingForInstanceDetails =+ providerName === LLMFactory.Bedrock &&+ instanceName !== DRAFT_INSTANCE_SENTINEL &&+ !instanceDetailsLoaded;const hasBaseUrlField = creds.baseUrl !== undefined;const ready =- (!requiresApiKey || !!creds.apiKey) &&+ !waitingForInstanceDetails &&+ (!requiresApiKey || hasApiKey) &&(!hasBaseUrlField || !!creds.baseUrl);if (!ready) return;hasAutoFetchedRef.current = true;@@ -295,6 +315,10 @@ export function useModelsCatalog({interface UseModelsDerivedArgs {catalog: IProviderModelItem[];instanceModels: IInstanceModel[] | undefined;+ /** True while the saved instance model query is fetching. */+ instanceModelsLoading: boolean;+ /** True only when the saved instance model query completed successfully. */+ instanceModelsSucceeded: boolean;/*** Locally-added models for a draft (unsaved) instance. The hook uses* this list as the "instance models" source when `isDraftInstance` is@@ -316,6 +340,8 @@ interface UseModelsDerivedArgs {export function useModelsDerived({catalog,instanceModels,+ instanceModelsLoading,+ instanceModelsSucceeded,draftModels,isDraftInstance,onInstanceModelsChange,@@ -403,36 +429,28 @@ export function useModelsDerived({onEditedRef.current = onInstanceModelsEdited;});- // Track the previous set of instance model names so we can tell- // "patch" (same names, different data) apart from "add/remove"- // (different names). Only the patch case needs to fire the host-side- // baseline-update callback so the next blur auto-save short-circuits.- const prevNamesRef = useRef<Set<string>>(new Set());-// Push the latest per-instance model list up to the host so its- // auto-save can include `model_info` in the payload. When the change- // is purely a patch (same names, different data), also notify the- // host via `onInstanceModelsEdited` so it can absorb the model_info- // diff into its last-saved baseline — without this signal, the next- // blur would signature-mismatch and fire a redundant PUT carrying- // the already-PATCH-saved model_info. Adds/removes intentionally- // skip this signal so the next blur still carries the updated list- // into PUT (the standard sync path for the instance's model_info).+ // save payload can include `model_info`.useEffect(() => {- const currentNames = new Set(instanceItems.map((m) => m.name));- const prevNames = prevNamesRef.current;- const isPatch =- currentNames.size > 0 &&- currentNames.size === prevNames.size &&- Array.from(currentNames).every((n) => prevNames.has(n));-onChangeRef.current?.(buildModelInfo(instanceItems));- if (isPatch) {+ }, [instanceItems]);++ // Saved instance models come from the backend, both after the initial+ // query and after mutation-driven refetches. Once that authoritative+ // snapshot is ready, absorb it into the host's saved baseline so an+ // unchanged card does not look dirty solely because model_info loaded+ // after the instance details. Draft models remain local and must still+ // be included in the first save.+ useEffect(() => {+ if (!isDraftInstance && !instanceModelsLoading && instanceModelsSucceeded) {onEditedRef.current?.();}-- prevNamesRef.current = currentNames;- }, [instanceItems]);+ }, [+ instanceItems,+ instanceModelsLoading,+ instanceModelsSucceeded,+ isDraftInstance,+ ]);return { instanceItems, models, addedSet };}@@ -472,26 +490,8 @@ interface UseModelVerifyArgs {resolveCreds: () => ResolvedCreds;instanceModels: IInstanceModel[] | undefined;instance?: IProviderInstance;- /**- * Host card's current form values. Required when `verifyTransform` is- * supplied so the transform can map provider-specific field names- * (e.g. PaddleOCR's `paddleocr_api_url`) onto the structured `api_key`- * object the verify endpoint expects.- */getFormValues?: () => Record<string, any>;- /**- * Provider-specific transform that maps form values onto the verify- * payload's `api_key` / `base_url` / `region`. When present it takes- * precedence over the generic `resolveCreds()` mapping. The- * `modelInfo` it returns is ignored - per-model verify always sends- * only the single model being verified.- */- verifyTransform?: (values: Record<string, any>) => {- apiKey: string | object | Record<string, any>;- baseUrl?: string;- region?: string;- modelInfo?: IModelInfo[];- };+ verifyTransform?: ModelsSectionProps['verifyTransform'];}/**@@ -520,8 +520,7 @@ function buildVerifyArgs(let region: string | undefined;if (verifyTransform) {- const formValues = getFormValues?.() ?? {};- const transformed = verifyTransform(formValues);+ const transformed = verifyTransform(getFormValues?.() ?? {});apiKey = transformed.apiKey;baseUrl = transformed.baseUrl;region = transformed.region;@@ -937,9 +936,8 @@ export function useModelEdit({// Persist edits to an existing model. For drafts the backend has no// instance yet, so we update the local `draftModels` list instead of- // calling PATCH. For saved cards the instance-models cache is patched- // so the UI reflects the new values immediately, before the PATCH's- // invalidation refetches.+ // calling PATCH. For saved cards the cache changes only after the backend+ // accepts the edit; the PATCH hook then refetches the complete snapshot.const handleEditSubmit = async (item: IProviderModelItem) => {if (!editingModel) return;const targetName = editingModel.name;@@ -956,29 +954,7 @@ export function useModelEdit({return;}- queryClient.setQueryData<IInstanceModel[]>(- LlmKeys.instanceModels(providerName, instanceName),- (prev) => {- if (!prev) return prev;- const idx = prev.findIndex((m) => m.name === targetName);- if (idx === -1) return prev;- const next = [...prev];- const existing = next[idx];- next[idx] = {- ...existing,- max_tokens: item.max_tokens ?? 0,- model_type: item.model_types ?? [],- is_tools: hasToolFeature(item.features),- extra: {- is_tools: hasToolFeature(item.features),- ...(item.extra ?? {}),- },- };- return next;- },- );-- await patchInstanceModel({+ const data = await patchInstanceModel({provider_name: providerName,instance_name: instanceName,model_name: targetName,@@ -986,6 +962,29 @@ export function useModelEdit({model_type: item.model_types ?? [],extra: { is_tools: hasToolFeature(item.features), ...(item.extra ?? {}) },});+ if (data.code === 0) {+ queryClient.setQueryData<IInstanceModel[]>(+ LlmKeys.instanceModels(providerName, instanceName),+ (prev) => {+ if (!prev) return prev;+ const idx = prev.findIndex((m) => m.name === targetName);+ if (idx === -1) return prev;+ const next = [...prev];+ const existing = next[idx];+ next[idx] = {+ ...existing,+ max_tokens: item.max_tokens ?? 0,+ model_type: item.model_types ?? [],+ is_tools: hasToolFeature(item.features),+ extra: {+ is_tools: hasToolFeature(item.features),+ ...(item.extra ?? {}),+ },+ };+ return next;+ },+ );+ }clearCatalogOverride(targetName);setEditingModel(null);};diff --git a/web/src/pages/user-setting/setting-model/instance-card/models-section/index.tsx b/web/src/pages/user-setting/setting-model/instance-card/models-section/index.tsxindex 7ce973be11..55c1a9dcf3 100644--- a/web/src/pages/user-setting/setting-model/instance-card/models-section/index.tsx+++ b/web/src/pages/user-setting/setting-model/instance-card/models-section/index.tsx@@ -21,7 +21,13 @@ import { useCommonTranslation, useTranslate } from '@/hooks/common-hooks';import { useFetchInstanceModels } from '@/hooks/use-llm-request';import { IProviderModelItem } from '@/interfaces/request/llm';import { Loader2, Plus, Search, ShieldCheck } from 'lucide-react';-import { useCallback, useEffect, useRef, useState } from 'react';+import {+ useCallback,+ useEffect,+ useLayoutEffect,+ useRef,+ useState,+} from 'react';import { useTranslation } from 'react-i18next';import { AddCustomModelDialog } from '../add-custom-model-dialog';import { mapModelKey } from '../available-models';@@ -56,6 +62,7 @@ export function ModelsSection(props: ModelsSectionProps) {onBlurSuppressChange,onInstanceModelsChange,onInstanceModelsEdited,+ onInstanceModelsStatusChange,} = props;const isDraftInstance =@@ -71,10 +78,25 @@ export function ModelsSection(props: ModelsSectionProps) {const currentCreds = resolveCreds();// 2. Per-instance saved models (shared by catalog, derived, verify).- const { data: instanceModels } = useFetchInstanceModels(- providerName,- instanceName,- );+ const {+ data: instanceModels,+ loading: instanceModelsLoading,+ isSuccess: instanceModelsSucceeded,+ } = useFetchInstanceModels(providerName, instanceName);++ useLayoutEffect(() => {+ if (+ !isDraftInstance &&+ (instanceModelsLoading || !instanceModelsSucceeded)+ ) {+ onInstanceModelsStatusChange?.(false);+ }+ }, [+ instanceModelsLoading,+ instanceModelsSucceeded,+ isDraftInstance,+ onInstanceModelsStatusChange,+ ]);// 3. Upstream catalog + auto-fetch on mount.const {@@ -150,12 +172,25 @@ export function ModelsSection(props: ModelsSectionProps) {const { instanceItems, models, addedSet } = useModelsDerived({catalog,instanceModels,+ instanceModelsLoading,+ instanceModelsSucceeded,draftModels,isDraftInstance,onInstanceModelsChange,onInstanceModelsEdited,});+ useEffect(() => {+ if (!isDraftInstance && !instanceModelsLoading && instanceModelsSucceeded) {+ onInstanceModelsStatusChange?.(true);+ }+ }, [+ instanceModelsLoading,+ instanceModelsSucceeded,+ isDraftInstance,+ onInstanceModelsStatusChange,+ ]);+// 5. Search + tag filter.const { search, tag, setSearch, setTag, filteredModels, allTags } =useModelsFilter(models);diff --git a/web/src/pages/user-setting/setting-model/instance-card/models-section/interface.ts b/web/src/pages/user-setting/setting-model/instance-card/models-section/interface.tsindex d7327e7941..aff8af7e76 100644--- a/web/src/pages/user-setting/setting-model/instance-card/models-section/interface.ts+++ b/web/src/pages/user-setting/setting-model/instance-card/models-section/interface.ts@@ -93,25 +93,14 @@ export interface ModelsSectionProps {*/onInstanceModelsChange?: (modelInfo: IModelInfo[]) => void;/**- * Optional callback fired when the per-instance model list changes- * in a way that does NOT need the host to re-sync via its own- * auto-save — i.e. an existing model was patched (max_tokens /- * model_type / features changed via the edit dialog) but the model- * set stayed the same.- *- * The PATCH endpoint already persisted the change server-side, so- * the host uses this signal to absorb the resulting model_info diff- * into its last-saved baseline. The next blur-driven auto-save will- * then short-circuit as a no-op (signature matches), avoiding a- * redundant PUT that re-sends the already-saved model_info.- *- * Pair with `onInstanceModelsChange`: that callback fires for every- * change (add/remove/patch) so the host can keep its `modelInfoRef`- * current, while `onInstanceModelsEdited` fires ONLY for patches so- * the host can suppress its next auto-save for an already-persisted- * change.+ * Optional callback fired after saved-instance model state is confirmed by+ * the backend, either from a query result or a successful edit. The host+ * uses it to absorb `model_info` into its last-saved baseline, avoiding a+ * redundant update for data that the backend has already persisted.*/onInstanceModelsEdited?: () => void;+ /** Reports whether the saved instance model snapshot is authoritative. */+ onInstanceModelsStatusChange?: (ready: boolean) => void;}export interface ModelTypeBadgesProps {diff --git a/web/src/pages/user-setting/setting-model/instance-card/provider-instance-card.tsx b/web/src/pages/user-setting/setting-model/instance-card/provider-instance-card.tsxindex e9de6e7c9f..8fbab965ad 100644--- a/web/src/pages/user-setting/setting-model/instance-card/provider-instance-card.tsx+++ b/web/src/pages/user-setting/setting-model/instance-card/provider-instance-card.tsx@@ -92,6 +92,7 @@ const GenericProviderInstanceCard = forwardRef<// via `setModelInfo`, read by the payload builder.const { t } = useTranslation();const modelInfoRef = useRef<IModelInfo[]>([]);+ const modelsLoadedRef = useRef(false);// Provider-specific config: carries `verifyTransform` / `submitTransform`// for providers whose form field names don't map directly onto@@ -194,6 +195,10 @@ const GenericProviderInstanceCard = forwardRef<// catch it). For both drafts and saved cards, run the form's// own validation so errors surface in the UI.if (isDraft && !draftName.trim()) return false;+ if (!isDraft && instanceDetails && !modelsLoadedRef.current) {+ message.error(t('setting.selectModelBeforeSave'));+ return false;+ }// List-model providers (list picker) require at least one selected model.if (LIST_MODEL_PROVIDERS.has(providerName) &&@@ -208,7 +213,15 @@ const GenericProviderInstanceCard = forwardRef<getSavePayload,markSaved,}),- [isDraft, draftName, getSavePayload, markSaved, providerName, t],+ [+ isDraft,+ draftName,+ getSavePayload,+ markSaved,+ providerName,+ t,+ instanceDetails,+ ],);return (@@ -247,6 +260,9 @@ const GenericProviderInstanceCard = forwardRef<instance={instance}instanceDetailsLoaded={Boolean(instanceDetails)}modelInfoRef={modelInfoRef}+ onInstanceModelsStatusChange={(ready) => {+ modelsLoadedRef.current = ready;+ }}draftName={draftName}open={open}setOpen={setOpen}diff --git a/web/src/pages/user-setting/setting-model/provider-schema/types.ts b/web/src/pages/user-setting/setting-model/provider-schema/types.tsindex fcc65fd1e1..47e45793eb 100644--- a/web/src/pages/user-setting/setting-model/provider-schema/types.ts+++ b/web/src/pages/user-setting/setting-model/provider-schema/types.ts@@ -147,8 +147,8 @@ export interface ProviderConfig {* `base_url` fallback on top.** When absent, the generic echo path runs: `unwrapApiKey` lifts the- * bare `api_key` plus `API_KEY_NESTED_FIELDS` (`group_id` /- * `api_version` / `provider_order`).+ * bare `api_key` plus the provider-specific fields registered in+ * `API_KEY_NESTED_FIELDS`.*/echoTransform?: (instance: Record<string, any>) => Record<string, any>;/**