diff --git a/easyflow-api/easyflow-api-admin/src/main/java/tech/easyflow/admin/controller/ai/ModelController.java b/easyflow-api/easyflow-api-admin/src/main/java/tech/easyflow/admin/controller/ai/ModelController.java index d9728ac5..d2dfa1b5 100644 --- a/easyflow-api/easyflow-api-admin/src/main/java/tech/easyflow/admin/controller/ai/ModelController.java +++ b/easyflow-api/easyflow-api-admin/src/main/java/tech/easyflow/admin/controller/ai/ModelController.java @@ -13,6 +13,7 @@ import tech.easyflow.ai.entity.ModelProvider; import tech.easyflow.ai.entity.table.ModelTableDef; import tech.easyflow.ai.mapper.ModelMapper; import tech.easyflow.ai.service.ModelService; +import tech.easyflow.ai.service.capability.ModelCapabilityResolution; import tech.easyflow.common.domain.Result; import tech.easyflow.common.entity.LoginAccount; import tech.easyflow.common.satoken.util.SaTokenUtil; @@ -92,6 +93,21 @@ public class ModelController extends BaseCurdController { return Result.ok(modelService.verifyModelConfig(model)); } + /** + * 根据模型 ID 返回自动识别的类型和能力。 + * + * @param providerId 供应商 ID + * @param modelName 模型 ID + * @return 模型能力识别结果 + */ + @GetMapping("capabilities") + @SaCheckPermission("/api/v1/model/query") + public Result resolveCapabilities( + @RequestParam(required = false) BigInteger providerId, + @RequestParam String modelName) { + return Result.ok(modelService.resolveModelCapabilities(providerId, modelName)); + } + @PostMapping("/removeByEntity") @SaCheckPermission("/api/v1/model/remove") public Result removeByEntity(@RequestBody Model entity) { diff --git a/easyflow-modules/easyflow-module-agent/src/main/java/tech/easyflow/agent/runtime/AgentScopeChatModelConnectivityVerifier.java b/easyflow-modules/easyflow-module-agent/src/main/java/tech/easyflow/agent/runtime/AgentScopeChatModelConnectivityVerifier.java index 52a0187f..8658824e 100644 --- a/easyflow-modules/easyflow-module-agent/src/main/java/tech/easyflow/agent/runtime/AgentScopeChatModelConnectivityVerifier.java +++ b/easyflow-modules/easyflow-module-agent/src/main/java/tech/easyflow/agent/runtime/AgentScopeChatModelConnectivityVerifier.java @@ -15,9 +15,12 @@ import com.easyagents.agent.runtime.model.AgentModelSpec; import io.agentscope.core.message.ContentBlock; import io.agentscope.core.message.Msg; import io.agentscope.core.message.TextBlock; +import io.agentscope.core.message.ToolUseBlock; import io.agentscope.core.model.ChatResponse; import io.agentscope.core.model.ExecutionConfig; import io.agentscope.core.model.GenerateOptions; +import io.agentscope.core.model.ToolChoice; +import io.agentscope.core.model.ToolSchema; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -30,8 +33,12 @@ import tech.easyflow.common.web.exceptions.BusinessException; import java.time.Duration; import java.util.ArrayList; import java.util.Base64; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Locale; import java.util.Map; +import java.util.UUID; +import java.util.function.Supplier; /** * 使用 AgentScope 真实运行链路验证 Chat Model 与 VLM 连通性。 @@ -40,11 +47,13 @@ import java.util.Map; public class AgentScopeChatModelConnectivityVerifier implements ChatModelConnectivityVerifier { private static final Logger LOG = LoggerFactory.getLogger(AgentScopeChatModelConnectivityVerifier.class); + private static final String PROBE_TOOL_NAME = "easyflow_capability_probe"; + private static final String PROBE_NONCE_FIELD = "nonce"; + private static final String PROBE_IMAGE_FIELD = "imageCode"; + private static final String VERIFICATION_SYSTEM_PROMPT = "You are a model connectivity verification assistant."; /** 为未识别关闭思考扩展参数的推理模型保留足够的小额输出预算。 */ - private static final int MAX_TOKENS = 256; + private static final int MAX_TOKENS = 128; private static final Duration DEFAULT_PHASE_TIMEOUT = Duration.ofSeconds(60); - private static final String VERIFICATION_SYSTEM_PROMPT = - "You are verifying model connectivity. Follow the user's instruction exactly."; /** AgentScope 模型工厂。 */ private final AgentModelFactory modelFactory; @@ -52,12 +61,15 @@ public class AgentScopeChatModelConnectivityVerifier implements ChatModelConnect private final AgentScopeMessageAdapter messageAdapter; /** 单阶段最大等待时间。 */ private final Duration phaseTimeout; + /** 为每次工具能力探测生成随机校验值。 */ + private final Supplier nonceSupplier; /** * 使用生产运行时组件创建验证器。 */ public AgentScopeChatModelConnectivityVerifier() { - this(new AgentScopeModelFactory(), new AgentScopeMessageAdapter(), DEFAULT_PHASE_TIMEOUT); + this(new AgentScopeModelFactory(), new AgentScopeMessageAdapter(), DEFAULT_PHASE_TIMEOUT, + () -> UUID.randomUUID().toString()); } /** @@ -71,60 +83,122 @@ public class AgentScopeChatModelConnectivityVerifier implements ChatModelConnect AgentModelFactory modelFactory, AgentScopeMessageAdapter messageAdapter, Duration phaseTimeout) { - this.modelFactory = modelFactory; - this.messageAdapter = messageAdapter; - this.phaseTimeout = phaseTimeout; + this(modelFactory, messageAdapter, phaseTimeout, () -> UUID.randomUUID().toString()); } /** - * 依次验证非流式基础连接与流式响应能力。 + * 使用可控随机值创建验证器,供能力探测测试使用。 + * + * @param modelFactory AgentScope 模型工厂 + * @param messageAdapter 消息适配器 + * @param phaseTimeout 单阶段最大等待时间 + * @param nonceSupplier 探测随机值生成器 + */ + AgentScopeChatModelConnectivityVerifier( + AgentModelFactory modelFactory, + AgentScopeMessageAdapter messageAdapter, + Duration phaseTimeout, + Supplier nonceSupplier) { + this.modelFactory = modelFactory; + this.messageAdapter = messageAdapter; + this.phaseTimeout = phaseTimeout; + this.nonceSupplier = nonceSupplier; + } + + /** + * 使用一次非流式 Chat 请求优先同时验证连接、视觉与工具调用能力。 + * + *

当兼容接口明确拒绝工具参数时,追加一次不带工具的连接兜底请求, + * 避免将可用的普通对话模型误判为连接失败。

* * @param model 已补齐供应商默认配置的模型 - * @return 双阶段验证结果 - * @throws BusinessException 非流式基础连接失败时抛出 + * @return 连接和工具能力验证结果 + * @throws BusinessException 连接或视觉验证失败时抛出 */ @Override public ChatModelVerificationResult verify(Model model) { AgentModelSpec modelSpec = AgentModelSpecMapper.fromModel(model); String effectiveHttpVersion = AgentHttpTransportProvider.resolveEffectivePolicy( modelSpec.getHttpVersionPolicy(), modelSpec.getBaseUrl()).name(); - AgentMessage verificationMessage = buildVerificationMessage(modelSpec.isSupportImage()); + String nonce = nonceSupplier.get(); try { - verifyPhase(modelSpec, verificationMessage, false); - } catch (BusinessException exception) { - LOG.error("AgentScope model base connectivity verification failed, modelId={}, httpPolicy={}", - model.getId(), effectiveHttpVersion, exception); - throw exception; + boolean supportTool = verifyProbeRequest(modelSpec, nonce); + return ChatModelVerificationResult.passed(effectiveHttpVersion, supportTool); } catch (Exception exception) { - LOG.error("AgentScope model base connectivity verification failed, modelId={}, httpPolicy={}", + if (isToolCapabilityRejection(exception)) { + LOG.info("Model endpoint rejected tool probe, fallback to plain connectivity, modelId={}", + model.getId()); + try { + verifyPlainRequest(modelSpec); + return ChatModelVerificationResult.passed(effectiveHttpVersion, false); + } catch (Exception fallbackException) { + LOG.error("AgentScope model fallback connectivity verification failed, modelId={}, httpPolicy={}", + model.getId(), effectiveHttpVersion, fallbackException); + throw new BusinessException( + 400, 1, "模型连接验证失败,请查看后端日志", fallbackException); + } + } + LOG.error("AgentScope model connectivity verification failed, modelId={}, httpPolicy={}", model.getId(), effectiveHttpVersion, exception); - throw new BusinessException(400, 1, "模型基础连接验证失败,请查看后端日志", exception); - } - - try { - verifyPhase(modelSpec, verificationMessage, true); - return ChatModelVerificationResult.passed(effectiveHttpVersion); - } catch (Exception exception) { - LOG.warn("AgentScope model streaming verification failed, modelId={}, httpPolicy={}", - model.getId(), effectiveHttpVersion, exception); - return ChatModelVerificationResult.streamingUnavailable(effectiveHttpVersion); + if (exception instanceof BusinessException businessException) { + throw businessException; + } + throw new BusinessException(400, 1, "模型连接验证失败,请查看后端日志", exception); } } /** - * 执行一次指定流式模式的模型请求并校验响应。 + * 执行携带无副作用工具 Schema 的能力探测请求。 + * + * @param modelSpec 运行时模型声明 + * @param nonce 本次探测随机值 + * @return 模型是否正确返回指定工具调用 + * @throws BusinessException 响应为空或视觉识别失败时抛出 + */ + private boolean verifyProbeRequest(AgentModelSpec modelSpec, String nonce) { + AgentMessage message = buildProbeMessage(modelSpec.isSupportImage(), nonce); + List responses = request( + modelSpec, + message, + List.of(buildProbeToolSchema(modelSpec.isSupportImage())), + new ToolChoice.Specific(PROBE_TOOL_NAME)); + boolean validToolCall = hasValidProbeToolCall( + responses, nonce, modelSpec.isSupportImage()); + validateProbeResponse(modelSpec.isSupportImage(), responses, validToolCall); + return validToolCall; + } + + /** + * 执行不带工具的兼容性兜底请求。 + * + * @param modelSpec 运行时模型声明 + * @throws BusinessException 响应为空或视觉识别失败时抛出 + */ + private void verifyPlainRequest(AgentModelSpec modelSpec) { + List responses = request( + modelSpec, + buildPlainVerificationMessage(modelSpec.isSupportImage()), + List.of(), + null); + validateTextResponse(modelSpec.isSupportImage(), aggregateText(responses)); + } + + /** + * 使用统一低成本参数执行一次非流式模型请求。 * * @param modelSpec 运行时模型声明 * @param verificationMessage 验证消息 - * @param stream 是否启用流式响应 - * @throws BusinessException 响应为空或 VLM 图片识别错误时抛出 + * @param tools 工具 Schema + * @param toolChoice 工具选择策略 + * @return 模型响应片段 */ - private void verifyPhase(AgentModelSpec modelSpec, - AgentMessage verificationMessage, - boolean stream) { + private List request(AgentModelSpec modelSpec, + AgentMessage verificationMessage, + List tools, + ToolChoice toolChoice) { AgentGenerationOptions generationOptions = new AgentGenerationOptions(); - generationOptions.setStream(stream); + generationOptions.setStream(false); generationOptions.setThinkingEnabled(false); disableOpenAiCompatibleThinking(modelSpec, generationOptions); generationOptions.setMaxTokens(MAX_TOKENS); @@ -132,24 +206,23 @@ public class AgentScopeChatModelConnectivityVerifier implements ChatModelConnect List messages = List.of( messageAdapter.toMsg(AgentMessage.text( - AgentMessageRole.SYSTEM, - VERIFICATION_SYSTEM_PROMPT)), + AgentMessageRole.SYSTEM, VERIFICATION_SYSTEM_PROMPT)), messageAdapter.toMsg(verificationMessage)); - GenerateOptions requestOptions = GenerateOptions.builder() - .stream(stream) + GenerateOptions.Builder requestBuilder = GenerateOptions.builder() + .stream(false) .maxTokens(MAX_TOKENS) .executionConfig(ExecutionConfig.builder() .timeout(phaseTimeout) .maxAttempts(1) - .build()) - .build(); - List responses = agentScopeModel - .stream(messages, List.of(), requestOptions) + .build()); + if (toolChoice != null) { + requestBuilder.toolChoice(toolChoice); + } + return agentScopeModel + .stream(messages, tools, requestBuilder.build()) .timeout(phaseTimeout) .collectList() .block(phaseTimeout.plusSeconds(1)); - String responseText = aggregateText(responses); - validateResponse(modelSpec.isSupportImage(), responseText); } /** @@ -182,19 +255,47 @@ public class AgentScopeChatModelConnectivityVerifier implements ChatModelConnect } /** - * 创建文字模型或 VLM 的最小验证消息。 + * 创建同时验证连接、视觉和工具调用的消息。 * * @param supportImage 是否验证图片理解能力 - * @return 验证消息 + * @param nonce 本次探测随机值 + * @return 工具能力探测消息 */ - private AgentMessage buildVerificationMessage(boolean supportImage) { + private AgentMessage buildProbeMessage(boolean supportImage, String nonce) { + String prompt = supportImage + ? "请调用 " + PROBE_TOOL_NAME + " 工具,将 nonce 设置为“" + nonce + + "”,并将图片中的验证码设置为 imageCode。不要直接回答。" + : "请调用 " + PROBE_TOOL_NAME + " 工具,并将 nonce 设置为“" + nonce + + "”。不要直接回答。"; + return buildVerificationMessage(prompt, supportImage); + } + + /** + * 创建不带工具的兼容性兜底消息。 + * + * @param supportImage 是否验证图片理解能力 + * @return 普通连接验证消息 + */ + private AgentMessage buildPlainVerificationMessage(boolean supportImage) { + String prompt = supportImage + ? "请直接输出图片中的内容,不要补充其他内容。" + : "请直接回复“你好”,不要补充其他内容。"; + return buildVerificationMessage(prompt, supportImage); + } + + /** + * 创建文字或 VLM 验证消息。 + * + * @param prompt 验证提示词 + * @param supportImage 是否附加验证图片 + * @return AgentScope 消息 + */ + private AgentMessage buildVerificationMessage(String prompt, boolean supportImage) { if (!supportImage) { - return AgentMessage.text( - AgentMessageRole.USER, - "请直接回复“你好”,不要补充其他内容。"); + return AgentMessage.text(AgentMessageRole.USER, prompt); } List blocks = new ArrayList<>(); - blocks.add(new AgentTextBlock("请直接输出图片中的内容,不要补充其他内容。")); + blocks.add(new AgentTextBlock(prompt)); AgentMediaBlock image = new AgentMediaBlock("image"); image.setMimeType("image/png"); image.setData(Base64.getEncoder().encodeToString(VlmVerificationImage.pngBytes())); @@ -205,6 +306,111 @@ public class AgentScopeChatModelConnectivityVerifier implements ChatModelConnect return message; } + /** + * 创建只声明参数、不绑定执行逻辑的探测工具 Schema。 + * + * @param supportImage 是否包含图片验证码参数 + * @return 探测工具 Schema + */ + private ToolSchema buildProbeToolSchema(boolean supportImage) { + Map properties = new LinkedHashMap<>(); + properties.put(PROBE_NONCE_FIELD, Map.of( + "type", "string", + "description", "原样返回用户提供的 nonce")); + List required = new ArrayList<>(); + required.add(PROBE_NONCE_FIELD); + if (supportImage) { + properties.put(PROBE_IMAGE_FIELD, Map.of( + "type", "string", + "description", "图片中的验证码")); + required.add(PROBE_IMAGE_FIELD); + } + return ToolSchema.builder() + .name(PROBE_TOOL_NAME) + .description("验证模型是否能生成结构化工具调用") + .parameters(Map.of( + "type", "object", + "properties", properties, + "required", required)) + .strict(false) + .build(); + } + + /** + * 判断响应是否包含参数正确的探测工具调用。 + * + * @param responses AgentScope 响应片段 + * @param nonce 本次探测随机值 + * @param supportImage 是否同时验证图片 + * @return 工具名和参数均正确返回 true + */ + private boolean hasValidProbeToolCall(List responses, + String nonce, + boolean supportImage) { + if (responses == null) { + return false; + } + for (ChatResponse response : responses) { + if (response == null || response.getContent() == null) { + continue; + } + for (ContentBlock block : response.getContent()) { + if (!(block instanceof ToolUseBlock toolUse) + || !PROBE_TOOL_NAME.equals(toolUse.getName()) + || toolUse.getInput() == null + || !nonce.equals(String.valueOf(toolUse.getInput().get(PROBE_NONCE_FIELD)))) { + continue; + } + if (!supportImage || VlmVerificationImage.VERIFICATION_CODE.equals( + normalizeVerificationText(String.valueOf( + toolUse.getInput().get(PROBE_IMAGE_FIELD))))) { + return true; + } + } + } + return false; + } + + /** + * 校验探测响应是否足以证明连接和视觉能力。 + * + * @param supportImage 是否验证图片理解能力 + * @param responses AgentScope 响应片段 + * @param validToolCall 是否包含正确工具调用 + * @throws BusinessException 响应为空或图片识别错误时抛出 + */ + private void validateProbeResponse(boolean supportImage, + List responses, + boolean validToolCall) { + if (validToolCall) { + return; + } + String responseText = aggregateText(responses); + if (responseText != null && !responseText.isBlank()) { + validateTextResponse(supportImage, responseText); + return; + } + if (hasAnyContent(responses) && !supportImage) { + return; + } + throw new BusinessException("模型未返回有效内容"); + } + + /** + * 判断模型是否返回任意内容块。 + * + * @param responses AgentScope 响应片段 + * @return 存在内容块返回 true + */ + private boolean hasAnyContent(List responses) { + if (responses == null) { + return false; + } + return responses.stream() + .filter(response -> response != null && response.getContent() != null) + .anyMatch(response -> !response.getContent().isEmpty()); + } + /** * 聚合流式响应中的全部文本增量。 * @@ -236,7 +442,7 @@ public class AgentScopeChatModelConnectivityVerifier implements ChatModelConnect * @param responseText 聚合后的响应文本 * @throws BusinessException 响应为空或图片识别结果不匹配时抛出 */ - private void validateResponse(boolean supportImage, String responseText) { + private void validateTextResponse(boolean supportImage, String responseText) { if (responseText == null || responseText.isBlank()) { throw new BusinessException("模型未返回有效内容"); } @@ -278,4 +484,34 @@ public class AgentScopeChatModelConnectivityVerifier implements ChatModelConnect ? normalized : normalized.substring(0, 160) + "..."; } + + /** + * 判断异常是否明确来自工具或工具选择参数不兼容。 + * + * @param exception 模型调用异常 + * @return 工具参数被拒绝返回 true + */ + private boolean isToolCapabilityRejection(Throwable exception) { + Throwable current = exception; + while (current != null) { + String message = current.getMessage(); + if (message != null) { + String normalized = message.toLowerCase(Locale.ROOT); + if (normalized.contains("tool_choice") + || normalized.contains("tool choice") + || normalized.contains("tool_call") + || normalized.contains("tool call") + || normalized.contains("tools parameter") + || normalized.contains("function calling") + || (normalized.contains("tools") + && (normalized.contains("unsupported") + || normalized.contains("not support") + || normalized.contains("invalid")))) { + return true; + } + } + current = current.getCause(); + } + return false; + } } diff --git a/easyflow-modules/easyflow-module-agent/src/test/java/tech/easyflow/agent/runtime/AgentScopeChatModelConnectivityVerifierTest.java b/easyflow-modules/easyflow-module-agent/src/test/java/tech/easyflow/agent/runtime/AgentScopeChatModelConnectivityVerifierTest.java index 26b90cc2..6a62bc99 100644 --- a/easyflow-modules/easyflow-module-agent/src/test/java/tech/easyflow/agent/runtime/AgentScopeChatModelConnectivityVerifierTest.java +++ b/easyflow-modules/easyflow-module-agent/src/test/java/tech/easyflow/agent/runtime/AgentScopeChatModelConnectivityVerifierTest.java @@ -9,8 +9,10 @@ import io.agentscope.core.message.ImageBlock; import io.agentscope.core.message.Msg; import io.agentscope.core.message.MsgRole; import io.agentscope.core.message.TextBlock; +import io.agentscope.core.message.ToolUseBlock; import io.agentscope.core.model.ChatResponse; import io.agentscope.core.model.GenerateOptions; +import io.agentscope.core.model.ToolChoice; import io.agentscope.core.model.ToolSchema; import org.junit.Assert; import org.junit.Test; @@ -27,101 +29,102 @@ import java.time.Duration; import java.util.ArrayList; import java.util.Base64; import java.util.List; +import java.util.Map; /** - * AgentScope 双阶段模型连通性验证测试。 + * AgentScope 单次优先模型连接与工具能力验证测试。 */ public class AgentScopeChatModelConnectivityVerifierTest { - /** - * 验证非流式与流式阶段按顺序执行并返回通过状态。 - */ - @Test - public void shouldPassWhenBothPhasesReturnText() { - RecordingModelFactory factory = new RecordingModelFactory( - Flux.just(response("你好")), - Flux.just(response("你"), response("好"))); - AgentScopeChatModelConnectivityVerifier verifier = verifier(factory); - - ChatModelVerificationResult result = verifier.verify(model(false)); - - Assert.assertEquals(ModelVerificationStatus.PASSED, result.getStatus()); - Assert.assertEquals("HTTP_1_1", result.getEffectiveHttpVersion()); - Assert.assertEquals(List.of(false, true), factory.getFactoryStreams()); - Assert.assertEquals(List.of(false, true), factory.getRequestStreams()); - Assert.assertEquals(List.of(MsgRole.SYSTEM, MsgRole.USER), factory.getMessageRoles().get(0)); - Assert.assertEquals(List.of(false, false), factory.getEnableThinkingValues()); - Assert.assertEquals(List.of(false, false), factory.getChatTemplateThinkingValues()); - } + private static final String TEST_NONCE = "probe-nonce"; /** - * 验证基础连接通过而流式阶段失败时返回部分通过结果。 + * 验证一次请求正确返回工具调用时同时确认连接和工具能力。 */ @Test - public void shouldReturnPartialWhenStreamingPhaseFails() { + public void shouldPassConnectionAndToolProbeInOneRequest() { RecordingModelFactory factory = new RecordingModelFactory( - Flux.just(response("你好")), - Flux.error(new IllegalStateException("stream failed"))); + Flux.just(toolResponse(TEST_NONCE, null))); ChatModelVerificationResult result = verifier(factory).verify(model(false)); - Assert.assertEquals(ModelVerificationStatus.PARTIAL, result.getStatus()); + Assert.assertEquals(ModelVerificationStatus.PASSED, result.getStatus()); Assert.assertEquals(ModelVerificationStatus.PASSED, result.getNonStreaming()); - Assert.assertEquals(ModelVerificationStatus.FAILED, result.getStreaming()); - Assert.assertTrue(result.getMessage().contains("流式响应不可用")); + Assert.assertEquals(ModelVerificationStatus.SKIPPED, result.getStreaming()); + Assert.assertEquals(Boolean.TRUE, result.getSupportTool()); + Assert.assertEquals("验证通过", result.getMessage()); + Assert.assertEquals(List.of(false), factory.getFactoryStreams()); + Assert.assertEquals(List.of(false), factory.getRequestStreams()); + Assert.assertEquals(List.of(1), factory.getToolCounts()); + Assert.assertTrue(factory.getToolChoices().get(0) instanceof ToolChoice.Specific); + Assert.assertEquals(MsgRole.SYSTEM, factory.getMessageBatches().get(0).get(0).getRole()); + Assert.assertEquals(MsgRole.USER, factory.getMessageBatches().get(0).get(1).getRole()); } /** - * 验证基础连接失败时立即终止且返回业务失败。 + * 验证模型返回普通文本时连接通过但工具能力保持关闭。 */ @Test - public void shouldStopWhenNonStreamingPhaseFails() { + public void shouldPassConnectionAndMarkToolUnsupportedWhenTextReturned() { + RecordingModelFactory factory = new RecordingModelFactory( + Flux.just(textResponse("你好"))); + + ChatModelVerificationResult result = verifier(factory).verify(model(false)); + + Assert.assertEquals(ModelVerificationStatus.PASSED, result.getStatus()); + Assert.assertEquals(Boolean.FALSE, result.getSupportTool()); + Assert.assertEquals(List.of(1), factory.getToolCounts()); + } + + /** + * 验证接口明确拒绝工具参数时仅追加一次普通连接兜底。 + */ + @Test + public void shouldFallbackToPlainRequestWhenToolChoiceIsRejected() { + RecordingModelFactory factory = new RecordingModelFactory( + Flux.error(new IllegalArgumentException("tool_choice is unsupported")), + Flux.just(textResponse("你好"))); + + ChatModelVerificationResult result = verifier(factory).verify(model(false)); + + Assert.assertEquals(ModelVerificationStatus.PASSED, result.getStatus()); + Assert.assertEquals(Boolean.FALSE, result.getSupportTool()); + Assert.assertEquals(List.of(1, 0), factory.getToolCounts()); + Assert.assertTrue(factory.getToolChoices().get(0) instanceof ToolChoice.Specific); + Assert.assertNull(factory.getToolChoices().get(1)); + } + + /** + * 验证普通连接异常不会被工具兼容兜底掩盖。 + */ + @Test + public void shouldFailWhenConnectivityRequestFails() { RecordingModelFactory factory = new RecordingModelFactory( Flux.error(new IllegalStateException("connection failed"))); try { verifier(factory).verify(model(false)); - Assert.fail("Expected base connectivity verification failure"); + Assert.fail("Expected connectivity verification failure"); } catch (BusinessException exception) { - Assert.assertTrue(exception.getMessage().contains("基础连接验证失败")); + Assert.assertTrue(exception.getMessage().contains("连接验证失败")); Assert.assertFalse(exception.getMessage().contains("connection failed")); } - Assert.assertEquals(List.of(false), factory.getFactoryStreams()); + Assert.assertEquals(List.of(1), factory.getToolCounts()); } /** - * 验证流式阶段超时被归类为部分可用且不暴露底层异常。 + * 验证 VLM 在同一次工具调用中返回图片验证码。 */ @Test - public void shouldReturnPartialWhenStreamingPhaseTimesOut() { + public void shouldVerifyVlmAndToolCallInOneRequest() { RecordingModelFactory factory = new RecordingModelFactory( - Flux.just(response("你好")), - Flux.never()); - AgentScopeChatModelConnectivityVerifier verifier = new AgentScopeChatModelConnectivityVerifier( - factory, - new AgentScopeMessageAdapter(), - Duration.ofMillis(20)); - - ChatModelVerificationResult result = verifier.verify(model(false)); - - Assert.assertEquals(ModelVerificationStatus.PARTIAL, result.getStatus()); - Assert.assertEquals("连接成功,流式响应不可用,可关闭智能体的模型流式响应。", - result.getMessage()); - } - - /** - * 验证 VLM 使用 Base64 图片,并能聚合流式文本增量。 - */ - @Test - public void shouldVerifyVlmImageAndAggregateStreamingChunks() { - RecordingModelFactory factory = new RecordingModelFactory( - Flux.just(response("图片中的内容是“" + VlmVerificationImage.VERIFICATION_CODE + "”。")), - Flux.just(response("识别结果:58"), response("39"))); + Flux.just(toolResponse(TEST_NONCE, VlmVerificationImage.VERIFICATION_CODE))); ChatModelVerificationResult result = verifier(factory).verify(model(true)); Assert.assertEquals(ModelVerificationStatus.PASSED, result.getStatus()); - Msg message = factory.getMessages().get(0); + Assert.assertEquals(Boolean.TRUE, result.getSupportTool()); + Msg message = factory.getMessageBatches().get(0).get(1); ImageBlock image = message.getContent().stream() .filter(ImageBlock.class::isInstance) .map(ImageBlock.class::cast) @@ -154,7 +157,7 @@ public class AgentScopeChatModelConnectivityVerifierTest { } /** - * 创建使用测试工厂的验证器。 + * 创建使用固定随机值的验证器。 * * @param factory 记录型模型工厂 * @return 验证器 @@ -163,54 +166,73 @@ public class AgentScopeChatModelConnectivityVerifierTest { return new AgentScopeChatModelConnectivityVerifier( factory, new AgentScopeMessageAdapter(), - Duration.ofSeconds(2)); + Duration.ofSeconds(2), + () -> TEST_NONCE); } /** - * 创建单个文本响应片段。 + * 创建文本响应。 * * @param text 文本内容 * @return AgentScope 响应 */ - private ChatResponse response(String text) { + private ChatResponse textResponse(String text) { return ChatResponse.builder() .content(List.of(TextBlock.builder().text(text).build())) .build(); } /** - * 按阶段返回预设响应并记录调用参数的模型工厂。 + * 创建探测工具调用响应。 + * + * @param nonce 随机校验值 + * @param imageCode 图片验证码 + * @return AgentScope 响应 + */ + private ChatResponse toolResponse(String nonce, String imageCode) { + Map input = imageCode == null + ? Map.of("nonce", nonce) + : Map.of("nonce", nonce, "imageCode", imageCode); + return ChatResponse.builder() + .content(List.of(ToolUseBlock.builder() + .id("call-probe") + .name("easyflow_capability_probe") + .input(input) + .build())) + .build(); + } + + /** + * 按调用顺序返回预设响应并记录真实请求参数的模型工厂。 */ private static final class RecordingModelFactory implements AgentModelFactory { - /** 每个阶段的预设响应。 */ - private final List> phaseResponses; + /** 每次调用的预设响应。 */ + private final List> responses; /** 模型工厂收到的流式参数。 */ private final List factoryStreams = new ArrayList<>(); /** 模型请求收到的流式参数。 */ private final List requestStreams = new ArrayList<>(); - /** 模型请求收到的消息。 */ - private final List messages = new ArrayList<>(); - /** 各阶段请求消息的角色顺序。 */ - private final List> messageRoles = new ArrayList<>(); - /** OpenAI-compatible 请求中的思考开关。 */ - private final List enableThinkingValues = new ArrayList<>(); - /** GPUStack 模板参数中的思考开关。 */ - private final List chatTemplateThinkingValues = new ArrayList<>(); + /** 模型请求收到的消息批次。 */ + private final List> messageBatches = new ArrayList<>(); + /** 每次请求携带的工具数量。 */ + private final List toolCounts = new ArrayList<>(); + /** 每次请求使用的工具选择策略。 */ + private final List toolChoices = new ArrayList<>(); /** * 创建记录型模型工厂。 * - * @param phaseResponses 每个阶段的预设响应 + * @param responses 每次调用的预设响应 */ @SafeVarargs - private RecordingModelFactory(Flux... phaseResponses) { - this.phaseResponses = List.of(phaseResponses); + private RecordingModelFactory(Flux... responses) { + this.responses = List.of(responses); } /** - * 创建当前验证阶段的测试模型。 + * 创建当前验证请求使用的模型。 * * @param modelSpec 模型声明 * @param generationOptions 生成参数 @@ -220,19 +242,12 @@ public class AgentScopeChatModelConnectivityVerifierTest { public io.agentscope.core.model.Model create( AgentModelSpec modelSpec, AgentGenerationOptions generationOptions) { - int phaseIndex = factoryStreams.size(); + int requestIndex = factoryStreams.size(); factoryStreams.add(Boolean.TRUE.equals(generationOptions.getStream())); - enableThinkingValues.add( - generationOptions.getAdditionalBodyParams().get("enable_thinking")); - Object templateOptions = generationOptions.getAdditionalBodyParams() - .get("chat_template_kwargs"); - chatTemplateThinkingValues.add(templateOptions instanceof java.util.Map map - ? map.get("enable_thinking") - : null); - Flux responses = phaseResponses.get(phaseIndex); + Flux response = responses.get(requestIndex); return new io.agentscope.core.model.Model() { /** - * 返回预设响应并记录真实请求参数。 + * 返回预设响应并记录请求参数。 * * @param inputMessages 模型消息 * @param tools 工具声明 @@ -245,9 +260,10 @@ public class AgentScopeChatModelConnectivityVerifierTest { List tools, GenerateOptions options) { requestStreams.add(Boolean.TRUE.equals(options.getStream())); - messages.add(inputMessages.get(inputMessages.size() - 1)); - messageRoles.add(inputMessages.stream().map(Msg::getRole).toList()); - return responses; + messageBatches.add(List.copyOf(inputMessages)); + toolCounts.add(tools.size()); + toolChoices.add(options.getToolChoice()); + return response; } /** @@ -263,7 +279,7 @@ public class AgentScopeChatModelConnectivityVerifierTest { } /** - * 获取工厂流式参数记录。 + * 获取模型工厂流式参数。 * * @return 流式参数列表 */ @@ -272,7 +288,7 @@ public class AgentScopeChatModelConnectivityVerifierTest { } /** - * 获取请求流式参数记录。 + * 获取请求流式参数。 * * @return 流式参数列表 */ @@ -281,39 +297,30 @@ public class AgentScopeChatModelConnectivityVerifierTest { } /** - * 获取请求消息记录。 + * 获取每次请求的验证消息批次。 * - * @return 消息列表 + * @return 验证消息批次 */ - private List getMessages() { - return messages; + private List> getMessageBatches() { + return messageBatches; } /** - * 获取各阶段请求消息的角色顺序。 + * 获取工具数量。 * - * @return 消息角色顺序 + * @return 工具数量列表 */ - private List> getMessageRoles() { - return messageRoles; + private List getToolCounts() { + return toolCounts; } /** - * 获取 OpenAI-compatible 请求中的思考开关。 + * 获取工具选择策略。 * - * @return 各阶段思考开关 + * @return 工具选择策略列表 */ - private List getEnableThinkingValues() { - return enableThinkingValues; - } - - /** - * 获取 GPUStack 模板参数中的思考开关。 - * - * @return 各阶段模板思考开关 - */ - private List getChatTemplateThinkingValues() { - return chatTemplateThinkingValues; + private List getToolChoices() { + return toolChoices; } } } diff --git a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/entity/Model.java b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/entity/Model.java index 4f2f60f7..62d8307b 100644 --- a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/entity/Model.java +++ b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/entity/Model.java @@ -80,8 +80,8 @@ public class Model extends ModelBase { deepseekConfig.setThinkingProtocol("deepseek"); deepseekConfig.setNeedReasoningContentForToolMessage(Boolean.TRUE); deepseekConfig.setSupportImageBase64Only(getSupportImageB64Only()); - if (getSupportToolMessage() != null) { - deepseekConfig.setSupportToolMessage(getSupportToolMessage()); + if (getSupportTool() != null) { + deepseekConfig.setSupportToolMessage(getSupportTool()); } return new DeepseekChatModel(deepseekConfig); default: @@ -92,8 +92,8 @@ public class Model extends ModelBase { openAIChatConfig.setModel(checkAndGetModelName()); openAIChatConfig.setRequestPath(checkAndGetRequestPath()); openAIChatConfig.setSupportImageBase64Only(getSupportImageB64Only()); - if (getSupportToolMessage() != null) { - openAIChatConfig.setSupportToolMessage(getSupportToolMessage()); + if (getSupportTool() != null) { + openAIChatConfig.setSupportToolMessage(getSupportTool()); } return new OpenAIChatModel(openAIChatConfig); } diff --git a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/invoke/service/impl/UnifiedModelInvokeServiceImpl.java b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/invoke/service/impl/UnifiedModelInvokeServiceImpl.java index 0085d894..5b3ab4e3 100644 --- a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/invoke/service/impl/UnifiedModelInvokeServiceImpl.java +++ b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/invoke/service/impl/UnifiedModelInvokeServiceImpl.java @@ -77,10 +77,12 @@ public class UnifiedModelInvokeServiceImpl implements UnifiedModelInvokeService throw ModelInvokeException.badRequest("当前模型仅支持 base64 图片输入", "messages", "image_base64_only"); } } - if (request.getTools() != null && !request.getTools().isEmpty() && !Boolean.TRUE.equals(model.getSupportTool())) { + if (request.getTools() != null + && !request.getTools().isEmpty() + && Boolean.FALSE.equals(model.getSupportTool())) { throw ModelInvokeException.badRequest("当前模型不支持 tools 参数", "tools", "tool_not_supported"); } - if (hasToolMessage(messages) && !Boolean.TRUE.equals(model.getSupportToolMessage())) { + if (hasToolMessage(messages) && Boolean.FALSE.equals(model.getSupportTool())) { throw ModelInvokeException.badRequest("当前模型不支持 tool 消息透传", "messages", "tool_message_not_supported"); } } diff --git a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/ModelService.java b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/ModelService.java index cf38ffb4..60cfa99c 100644 --- a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/ModelService.java +++ b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/ModelService.java @@ -2,6 +2,7 @@ package tech.easyflow.ai.service; import com.mybatisflex.core.service.IService; import tech.easyflow.ai.entity.Model; +import tech.easyflow.ai.service.capability.ModelCapabilityResolution; import java.math.BigInteger; import java.util.List; @@ -19,6 +20,15 @@ public interface ModelService extends IService { Map verifyModelConfig(Model llm); + /** + * 根据供应商和模型 ID 自动解析模型能力。 + * + * @param providerId 供应商 ID + * @param modelName 模型 ID + * @return 模型能力识别结果 + */ + ModelCapabilityResolution resolveModelCapabilities(BigInteger providerId, String modelName); + Map>> getList(Model entity); void removeByEntity(Model entity); diff --git a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/impl/ModelServiceImpl.java b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/impl/ModelServiceImpl.java index 0bf5ff9d..afcf7abb 100644 --- a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/impl/ModelServiceImpl.java +++ b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/impl/ModelServiceImpl.java @@ -8,6 +8,7 @@ import com.easyagents.core.model.embedding.EmbeddingModel; import com.easyagents.core.model.rerank.RerankModel; import com.easyagents.core.store.VectorData; import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.core.update.UpdateChain; import com.mybatisflex.core.util.StringUtil; import com.mybatisflex.spring.service.impl.ServiceImpl; import org.slf4j.Logger; @@ -21,7 +22,11 @@ import tech.easyflow.ai.entity.ModelProvider; import tech.easyflow.ai.mapper.ModelMapper; import tech.easyflow.ai.service.ModelProviderService; import tech.easyflow.ai.service.ModelService; +import tech.easyflow.ai.service.capability.ModelCapabilityResolution; +import tech.easyflow.ai.service.capability.ModelCapabilityResolver; +import tech.easyflow.ai.service.capability.ModelCapabilitySource; import tech.easyflow.ai.service.verification.ChatModelConnectivityVerifier; +import tech.easyflow.ai.service.verification.ChatModelVerificationResult; import tech.easyflow.common.tree.Tree; import tech.easyflow.common.util.SqlOperatorsUtil; import tech.easyflow.common.util.SqlUtil; @@ -47,6 +52,10 @@ public class ModelServiceImpl extends ServiceImpl implements @Autowired ModelProviderService modelProviderService; + /** 统一模型能力解析器。 */ + @Autowired + private ModelCapabilityResolver modelCapabilityResolver; + @Resource private Cache cache; @@ -92,6 +101,18 @@ public class ModelServiceImpl extends ServiceImpl implements } + /** + * 根据供应商和模型 ID 自动解析模型能力。 + * + * @param providerId 供应商 ID + * @param modelName 模型 ID + * @return 模型能力识别结果 + */ + @Override + public ModelCapabilityResolution resolveModelCapabilities(BigInteger providerId, String modelName) { + return modelCapabilityResolver.resolve(resolveProviderType(providerId, null), modelName); + } + @Override public Map>> getList(Model entity) { Map>> result = new HashMap<>(); @@ -166,7 +187,15 @@ public class ModelServiceImpl extends ServiceImpl implements if (chatModelConnectivityVerifier == null) { throw new BusinessException("Agent 模型连通性验证组件未加载"); } - return chatModelConnectivityVerifier.verify(model).toMap(); + ChatModelVerificationResult result = chatModelConnectivityVerifier.verify(model); + if (result.getSupportTool() != null && model.getId() != null) { + UpdateChain updateChain = updateChain(); + updateChain.set(Model::getSupportTool, result.getSupportTool()); + updateChain.set(Model::getSupportToolMessage, result.getSupportTool()); + updateChain.eq(Model::getId, model.getId()); + updateChain.update(); + } + return result.toMap(); } @Override @@ -199,6 +228,7 @@ public class ModelServiceImpl extends ServiceImpl implements if (entity == null) { throw new BusinessException("模型配置不能为空"); } + applyAutoCapabilities(entity); if (entity.getPublishEnabled() == null) { entity.setPublishEnabled(Boolean.FALSE); } @@ -239,6 +269,101 @@ public class ModelServiceImpl extends ServiceImpl implements entity.setInvokeCode(invokeCode); } + /** + * 将自动识别结果写入待保存模型,并清理已下线的展示能力字段。 + * + * @param entity 待保存模型 + */ + private void applyAutoCapabilities(Model entity) { + String providerType = resolveProviderType(entity.getProviderId(), entity.getModelProvider()); + ModelCapabilityResolution resolution = modelCapabilityResolver.resolve( + providerType, entity.getModelName()); + boolean modelIdentityChanged = hasModelIdentityChanged(entity); + + if (resolution.getSource() != ModelCapabilitySource.DEFAULT) { + entity.setModelType(resolution.getModelType()); + applyResolvedChatCapabilities(entity, resolution); + } else if (StrUtil.isBlank(entity.getModelType())) { + entity.setModelType(Model.MODEL_TYPES[0]); + } + + if (!Model.MODEL_TYPES[0].equals(entity.getModelType())) { + entity.setSupportImage(Boolean.FALSE); + entity.setSupportThinking(Boolean.FALSE); + entity.setSupportTool(Boolean.FALSE); + } else if (modelIdentityChanged) { + // 切换模型后,仅将仍无法识别且未被用户设置的能力归零。 + entity.setSupportImage(Boolean.TRUE.equals(entity.getSupportImage())); + entity.setSupportThinking(Boolean.TRUE.equals(entity.getSupportThinking())); + entity.setSupportTool(Boolean.TRUE.equals(entity.getSupportTool())); + } + + // 视频、音频尚未接入模型调用链,保存时保持关闭。 + entity.setSupportVideo(Boolean.FALSE); + entity.setSupportAudio(Boolean.FALSE); + // tool 消息能力跟随工具调用能力,不再由前端单独配置。 + entity.setSupportToolMessage(entity.getSupportTool()); + } + + /** + * 判断更新请求是否切换了实际模型或供应商。 + * + * @param entity 待更新模型 + * @return 模型标识发生变化返回 true + */ + private boolean hasModelIdentityChanged(Model entity) { + if (entity.getId() == null) { + return false; + } + Model stored = modelMapper.selectOneById(entity.getId()); + if (stored == null) { + return false; + } + boolean modelChanged = StrUtil.isNotBlank(entity.getModelName()) + && !StrUtil.equalsIgnoreCase( + StrUtil.trim(entity.getModelName()), + StrUtil.trim(stored.getModelName())); + boolean providerChanged = entity.getProviderId() != null + && !Objects.equals(entity.getProviderId(), stored.getProviderId()); + return modelChanged || providerChanged; + } + + /** + * 使用识别结果补齐尚未明确配置的对话能力,保留用户手动设置。 + * + * @param entity 待保存模型 + * @param resolution 模型能力识别结果 + */ + private void applyResolvedChatCapabilities(Model entity, ModelCapabilityResolution resolution) { + if (entity.getSupportImage() == null && resolution.getSupportImage() != null) { + entity.setSupportImage(resolution.getSupportImage()); + } + if (entity.getSupportThinking() == null && resolution.getSupportThinking() != null) { + entity.setSupportThinking(resolution.getSupportThinking()); + } + if (entity.getSupportTool() == null && resolution.getSupportTool() != null) { + entity.setSupportTool(resolution.getSupportTool()); + } + } + + /** + * 获取模型配置对应的供应商类型。 + * + * @param providerId 供应商 ID + * @param provider 已加载的供应商对象 + * @return 供应商类型,供应商不存在时返回 null + */ + private String resolveProviderType(BigInteger providerId, ModelProvider provider) { + if (provider != null && StrUtil.isNotBlank(provider.getProviderType())) { + return provider.getProviderType(); + } + if (providerId == null) { + return null; + } + ModelProvider storedProvider = modelProviderService.getById(providerId); + return storedProvider == null ? null : storedProvider.getProviderType(); + } + @Override public List listInvokeModels() { QueryWrapper queryWrapper = QueryWrapper.create().eq(Model::getModelType, Model.MODEL_TYPES[0]); diff --git a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/verification/ChatModelVerificationResult.java b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/verification/ChatModelVerificationResult.java index 16e4b324..0abaed4f 100644 --- a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/verification/ChatModelVerificationResult.java +++ b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/verification/ChatModelVerificationResult.java @@ -4,7 +4,7 @@ import java.util.LinkedHashMap; import java.util.Map; /** - * Chat Model 与 VLM 的双阶段连通性验证结果。 + * Chat Model 与 VLM 的连接和工具能力验证结果。 */ public final class ChatModelVerificationResult { @@ -16,6 +16,8 @@ public final class ChatModelVerificationResult { private final ModelVerificationStatus streaming; /** 实际生效的 HTTP 版本策略。 */ private final String effectiveHttpVersion; + /** 当前端点是否通过工具调用探测。 */ + private final Boolean supportTool; /** 用户可见的简洁结果说明。 */ private final String message; @@ -26,48 +28,39 @@ public final class ChatModelVerificationResult { * @param nonStreaming 非流式验证状态 * @param streaming 流式验证状态 * @param effectiveHttpVersion 实际生效的 HTTP 版本策略 + * @param supportTool 当前端点是否通过工具调用探测 * @param message 用户可见结果说明 */ private ChatModelVerificationResult(ModelVerificationStatus status, ModelVerificationStatus nonStreaming, ModelVerificationStatus streaming, String effectiveHttpVersion, + Boolean supportTool, String message) { this.status = status; this.nonStreaming = nonStreaming; this.streaming = streaming; this.effectiveHttpVersion = effectiveHttpVersion; + this.supportTool = supportTool; this.message = message; } /** - * 创建双阶段全部通过的结果。 + * 创建一次连接与工具探测通过的结果。 * * @param effectiveHttpVersion 实际生效的 HTTP 版本策略 - * @return 全部通过结果 + * @param supportTool 当前端点是否通过工具调用探测 + * @return 验证通过结果 */ - public static ChatModelVerificationResult passed(String effectiveHttpVersion) { + public static ChatModelVerificationResult passed(String effectiveHttpVersion, + boolean supportTool) { return new ChatModelVerificationResult( ModelVerificationStatus.PASSED, ModelVerificationStatus.PASSED, - ModelVerificationStatus.PASSED, + ModelVerificationStatus.SKIPPED, effectiveHttpVersion, - "验证成功"); - } - - /** - * 创建基础连接通过但流式阶段失败的结果。 - * - * @param effectiveHttpVersion 实际生效的 HTTP 版本策略 - * @return 部分通过结果 - */ - public static ChatModelVerificationResult streamingUnavailable(String effectiveHttpVersion) { - return new ChatModelVerificationResult( - ModelVerificationStatus.PARTIAL, - ModelVerificationStatus.PASSED, - ModelVerificationStatus.FAILED, - effectiveHttpVersion, - "连接成功,流式响应不可用,可关闭智能体的模型流式响应。"); + supportTool, + "验证通过"); } /** @@ -106,6 +99,15 @@ public final class ChatModelVerificationResult { return effectiveHttpVersion; } + /** + * 获取工具调用探测结果。 + * + * @return 是否通过工具调用探测 + */ + public Boolean getSupportTool() { + return supportTool; + } + /** * 获取用户可见结果说明。 * @@ -126,6 +128,9 @@ public final class ChatModelVerificationResult { result.put("nonStreaming", nonStreaming.name()); result.put("streaming", streaming.name()); result.put("effectiveHttpVersion", effectiveHttpVersion); + if (supportTool != null) { + result.put("supportTool", supportTool); + } result.put("message", message); return result; } diff --git a/easyflow-modules/easyflow-module-ai/src/test/java/tech/easyflow/ai/service/impl/ModelServiceImplCapabilityOverrideTest.java b/easyflow-modules/easyflow-module-ai/src/test/java/tech/easyflow/ai/service/impl/ModelServiceImplCapabilityOverrideTest.java new file mode 100644 index 00000000..d6fc0adf --- /dev/null +++ b/easyflow-modules/easyflow-module-ai/src/test/java/tech/easyflow/ai/service/impl/ModelServiceImplCapabilityOverrideTest.java @@ -0,0 +1,80 @@ +package tech.easyflow.ai.service.impl; + +import org.junit.Assert; +import org.junit.Test; +import tech.easyflow.ai.entity.Model; +import tech.easyflow.ai.service.capability.ModelCapabilityResolution; +import tech.easyflow.ai.service.capability.ModelCapabilitySource; + +import java.lang.reflect.Method; + +/** + * 模型能力自动识别与手动覆盖合并规则测试。 + */ +public class ModelServiceImplCapabilityOverrideTest { + + /** + * 验证用户明确关闭的能力不会被模型库重新打开。 + * + * @throws ReflectiveOperationException 无法调用待测试方法时抛出 + */ + @Test + public void shouldPreserveExplicitCapabilityOverrides() throws ReflectiveOperationException { + Model model = new Model(); + model.setSupportImage(Boolean.FALSE); + model.setSupportThinking(Boolean.FALSE); + model.setSupportTool(Boolean.FALSE); + + applyResolvedCapabilities(model, detectedCapabilities()); + + Assert.assertEquals(Boolean.FALSE, model.getSupportImage()); + Assert.assertEquals(Boolean.FALSE, model.getSupportThinking()); + Assert.assertEquals(Boolean.FALSE, model.getSupportTool()); + } + + /** + * 验证空能力值会由模型库自动补齐。 + * + * @throws ReflectiveOperationException 无法调用待测试方法时抛出 + */ + @Test + public void shouldFillCapabilitiesWhenNotConfigured() throws ReflectiveOperationException { + Model model = new Model(); + + applyResolvedCapabilities(model, detectedCapabilities()); + + Assert.assertEquals(Boolean.TRUE, model.getSupportImage()); + Assert.assertEquals(Boolean.TRUE, model.getSupportThinking()); + Assert.assertEquals(Boolean.TRUE, model.getSupportTool()); + } + + /** + * 创建模型库已确认的对话能力。 + * + * @return 全部开启的模型能力 + */ + private ModelCapabilityResolution detectedCapabilities() { + return new ModelCapabilityResolution( + Model.MODEL_TYPES[0], + Boolean.TRUE, + Boolean.TRUE, + Boolean.TRUE, + ModelCapabilitySource.CATALOG); + } + + /** + * 调用服务内部的能力合并逻辑。 + * + * @param model 待合并模型 + * @param resolution 自动识别结果 + * @throws ReflectiveOperationException 无法调用待测试方法时抛出 + */ + private void applyResolvedCapabilities(Model model, ModelCapabilityResolution resolution) + throws ReflectiveOperationException { + ModelServiceImpl service = new ModelServiceImpl(); + Method method = ModelServiceImpl.class.getDeclaredMethod( + "applyResolvedChatCapabilities", Model.class, ModelCapabilityResolution.class); + method.setAccessible(true); + method.invoke(service, model, resolution); + } +} diff --git a/easyflow-ui-admin/app/src/api/ai/llm.ts b/easyflow-ui-admin/app/src/api/ai/llm.ts index 2f246bc1..7cc6766a 100644 --- a/easyflow-ui-admin/app/src/api/ai/llm.ts +++ b/easyflow-ui-admin/app/src/api/ai/llm.ts @@ -39,6 +39,22 @@ export async function verifyModelConfig(id: string) { export type ModelCapabilitySource = 'CATALOG' | 'DEFAULT' | 'RULE'; +export interface ModelCapabilityResolution { + detected: boolean; + modelType: 'chatModel' | 'embeddingModel' | 'rerankModel'; + source: ModelCapabilitySource; + supportImage?: boolean | null; + supportThinking?: boolean | null; + supportTool?: boolean | null; +} + +export async function resolveModelCapabilities(params: { + modelName: string; + providerId?: string; +}) { + return api.get('/api/v1/model/capabilities', { params }); +} + export interface RemoteModelDescriptor { addable: boolean; added: boolean; @@ -91,6 +107,7 @@ export interface ModelVerificationData { nonStreaming?: ModelVerificationStageStatus; status?: ModelVerificationStageStatus; streaming?: ModelVerificationStageStatus; + supportTool?: boolean; } export interface ModelInvokeConfigPayload { @@ -136,8 +153,9 @@ export interface llmType { groupName: string; invokeCode?: string; publishEnabled?: boolean; - supportTool?: boolean; - supportImage?: boolean; + supportThinking?: boolean | null; + supportTool?: boolean | null; + supportImage?: boolean | null; supportImageB64Only?: boolean; supportToolMessage?: boolean; added: boolean; diff --git a/easyflow-ui-admin/app/src/locales/langs/en-US/llm.json b/easyflow-ui-admin/app/src/locales/langs/en-US/llm.json index f5a19e26..362fa80b 100644 --- a/easyflow-ui-admin/app/src/locales/langs/en-US/llm.json +++ b/easyflow-ui-admin/app/src/locales/langs/en-US/llm.json @@ -53,6 +53,7 @@ "groupName": "GroupName", "provider": "供应商", "ability": "ModelAbility", + "abilityChangeTip": "Model capabilities are detected automatically from the model ID. Change them carefully, as incorrect settings may prevent the model from working.", "button": { "management": "Management", "test": "Test", @@ -67,7 +68,7 @@ "supportTool": "Tool", "supportAudio": "Audio", "supportVideo": "Video", - "supportImage": "Multimodal", + "supportImage": "Vision", "supportFree": "Free", "supportImageB64Only": "Base64 images only", "supportToolMessage": "SupportToolMessage" diff --git a/easyflow-ui-admin/app/src/locales/langs/zh-CN/llm.json b/easyflow-ui-admin/app/src/locales/langs/zh-CN/llm.json index 518ad873..fb9f4c67 100644 --- a/easyflow-ui-admin/app/src/locales/langs/zh-CN/llm.json +++ b/easyflow-ui-admin/app/src/locales/langs/zh-CN/llm.json @@ -50,6 +50,7 @@ "groupName": "分组名称", "provider": "供应商", "ability": "模型能力", + "abilityChangeTip": "模型能力已根据模型 ID 自动识别,请谨慎修改,错误配置可能导致模型无法正常使用。", "button": { "management": "管理", "test": "检测", @@ -64,7 +65,7 @@ "supportTool": "工具", "supportAudio": "音频", "supportVideo": "视频", - "supportImage": "多模态", + "supportImage": "视觉", "supportFree": "免费", "supportImageB64Only": "仅接受 Base64 图片", "supportToolMessage": "支持Tool消息" diff --git a/easyflow-ui-admin/app/src/views/ai/model/ActiveModelWorkspace.vue b/easyflow-ui-admin/app/src/views/ai/model/ActiveModelWorkspace.vue index 80e37e00..8667ad94 100644 --- a/easyflow-ui-admin/app/src/views/ai/model/ActiveModelWorkspace.vue +++ b/easyflow-ui-admin/app/src/views/ai/model/ActiveModelWorkspace.vue @@ -284,6 +284,9 @@ const handleVerify = async (row: llmType) => { const res = await verifyModelConfig(modelId); const feedback = resolveModelVerificationFeedback(res, row.modelType); + if (typeof res.data?.supportTool === 'boolean') { + row.supportTool = res.data.supportTool; + } setVerifyStatus(modelId, feedback.status); if (feedback.status === 'success') { diff --git a/easyflow-ui-admin/app/src/views/ai/model/AddModelModal.vue b/easyflow-ui-admin/app/src/views/ai/model/AddModelModal.vue index 43eb581a..ba8caaef 100644 --- a/easyflow-ui-admin/app/src/views/ai/model/AddModelModal.vue +++ b/easyflow-ui-admin/app/src/views/ai/model/AddModelModal.vue @@ -6,25 +6,32 @@ import { computed, reactive, ref, watch } from 'vue'; import { EasyFlowFormModal } from '@easyflow/common-ui'; import { IconifyIcon } from '@easyflow/icons'; +import { + ArrowDown, + ArrowUp, + InfoFilled, + Loading, +} from '@element-plus/icons-vue'; import { ElForm, ElFormItem, + ElIcon, ElInput, ElMessage, ElOption, ElSelect, + ElTooltip, } from 'element-plus'; +import { resolveModelCapabilities } from '#/api/ai/llm'; import { api } from '#/api/request'; import { $t } from '#/locales'; import { getDefaultModelAbility, + handleTagClick as handleTagClickUtil, syncTagSelectedStatus as syncTagSelectedStatusUtil, } from '#/views/ai/model/modelUtils/model-ability'; -import { - generateFeaturesFromModelAbility, - resetModelAbility, -} from '#/views/ai/model/modelUtils/model-ability-utils'; +import { resetModelAbility } from '#/views/ai/model/modelUtils/model-ability-utils'; type AgentHttpVersionPolicy = 'AUTO' | 'HTTP_1_1' | 'HTTP_2_PREFERRED'; type AgentSystemContentFormat = 'STRING' | 'TEXT_PARTS'; @@ -47,14 +54,14 @@ interface FormData { apiKey: string; endpoint: string; requestPath: string; - supportThinking: boolean; - supportTool: boolean; - supportImage: boolean; + supportThinking: boolean | null; + supportTool: boolean | null; + supportImage: boolean | null; supportAudio: boolean; supportFree: boolean; supportVideo: boolean; supportImageB64Only: boolean; - supportToolMessage: boolean; + supportToolMessage: boolean | null; options: ModelOptions; } @@ -82,6 +89,11 @@ const formDataRef = ref(); const isAdd = ref(true); const dialogVisible = ref(false); const btnLoading = ref(false); +const showAdvanced = ref(false); +const capabilityLoading = ref(false); +const autoDetectedModelType = ref(false); +let capabilityRequestSequence = 0; +let manuallyEditedModelName = ''; const formData = reactive({ modelType: '', @@ -94,14 +106,14 @@ const formData = reactive({ apiKey: '', endpoint: '', requestPath: '', - supportThinking: false, - supportTool: false, - supportImage: false, + supportThinking: null, + supportTool: null, + supportImage: null, supportAudio: false, supportFree: false, supportVideo: false, supportImageB64Only: false, - supportToolMessage: true, + supportToolMessage: null, options: { agentHttpVersionPolicy: 'AUTO', agentSystemContentFormat: 'STRING', @@ -151,11 +163,7 @@ const normalizeModelOptions = (options?: unknown): ModelOptions => { }; const modelAbility = ref(getDefaultModelAbility()); -const visibleModelAbility = computed(() => - modelAbility.value.filter( - (item) => item.field !== 'supportImageB64Only' || formData.supportImage, - ), -); +const visibleModelAbility = computed(() => modelAbility.value); type SelectableModelType = '' | 'embeddingModel' | 'rerankModel'; const selectedModelType = ref(''); @@ -175,11 +183,7 @@ const abilityIconMap: Record = { rerankModel: 'svg:data-center', thinking: 'svg:llm', tool: 'svg:wrench', - video: 'mdi:video-outline', image: 'mdi:image-outline', - audio: 'mdi:microphone-outline', - imageB64: 'mdi:file-image-outline', - toolMessage: 'mdi:hammer', }; const syncTagSelectedStatus = () => { @@ -188,35 +192,61 @@ const syncTagSelectedStatus = () => { const resetAbilitySelection = () => { resetModelAbility(modelAbility.value); + formData.supportThinking = false; + formData.supportTool = false; + formData.supportImage = false; + formData.supportToolMessage = false; syncTagSelectedStatus(); }; -const handleTagClick = (item: ModelAbilityItem) => { +const markCapabilitiesAsManuallyEdited = () => { + manuallyEditedModelName = formData.modelName.trim(); + capabilityRequestSequence += 1; + capabilityLoading.value = false; +}; + +const handleAbilityChipClick = (item: ModelAbilityItem) => { if (hasSpecialModelType.value) { return; } - item.selected = !item.selected; - formData[item.field] = item.selected; - if (item.field === 'supportImage' && !item.selected) { - formData.supportImageB64Only = false; - const base64Ability = modelAbility.value.find( - (ability) => ability.field === 'supportImageB64Only', - ); - if (base64Ability) base64Ability.selected = false; + markCapabilitiesAsManuallyEdited(); + handleTagClickUtil(item, formData); + if (item.field === 'supportTool') { + formData.supportToolMessage = formData.supportTool; } }; +const handleModelNameInput = () => { + manuallyEditedModelName = ''; + capabilityRequestSequence += 1; + capabilityLoading.value = false; + autoDetectedModelType.value = false; + selectedModelType.value = ''; + formData.supportThinking = null; + formData.supportTool = null; + formData.supportImage = null; + formData.supportToolMessage = null; + syncTagSelectedStatus(); +}; + const handleModelTypeChipClick = ( modelType: Exclude, ) => { const nextType = selectedModelType.value === modelType ? '' : modelType; + markCapabilitiesAsManuallyEdited(); + autoDetectedModelType.value = false; selectedModelType.value = nextType; if (nextType) { resetAbilitySelection(); + } else { + formData.supportThinking = null; + formData.supportTool = null; + formData.supportImage = null; + formData.supportToolMessage = null; + syncTagSelectedStatus(); } }; -const isAbilityChipDisabled = () => hasSpecialModelType.value; const getAbilityIcon = (value: string) => abilityIconMap[value] || 'svg:llm'; const resolveModelType = (): FormData['modelType'] => { @@ -244,14 +274,14 @@ const resetFormData = () => { apiKey: '', endpoint: '', requestPath: '', - supportThinking: false, - supportTool: false, + supportThinking: null, + supportTool: null, supportAudio: false, supportVideo: false, - supportImage: false, + supportImage: null, supportImageB64Only: false, supportFree: false, - supportToolMessage: true, + supportToolMessage: null, options: normalizeModelOptions(), }); }; @@ -261,6 +291,9 @@ defineExpose({ isAdd.value = true; formDataRef.value?.resetFields(); resetFormData(); + showAdvanced.value = false; + autoDetectedModelType.value = false; + manuallyEditedModelName = ''; selectedModelType.value = normalizeSelectableModelType(modelType); if (selectedModelType.value) { resetAbilitySelection(); @@ -274,6 +307,9 @@ defineExpose({ dialogVisible.value = true; isAdd.value = false; resetFormData(); + showAdvanced.value = false; + autoDetectedModelType.value = false; + manuallyEditedModelName = ''; Object.assign(formData, { id: item.id, modelType: item.modelType || '', @@ -286,15 +322,14 @@ defineExpose({ endpoint: item.endpoint || '', requestPath: item.requestPath || '', apiKey: item.apiKey || '', - supportThinking: item.supportThinking || false, + supportThinking: item.supportThinking ?? null, supportAudio: item.supportAudio || false, - supportImage: item.supportImage || false, + supportImage: item.supportImage ?? null, supportImageB64Only: item.supportImageB64Only || false, supportVideo: item.supportVideo || false, - supportTool: item.supportTool || false, + supportTool: item.supportTool ?? null, supportFree: item.supportFree || false, - supportToolMessage: - item.supportToolMessage === undefined ? true : item.supportToolMessage, + supportToolMessage: item.supportToolMessage ?? null, options: normalizeModelOptions(item.options), }); selectedModelType.value = normalizeSelectableModelType(item.modelType); @@ -307,9 +342,65 @@ defineExpose({ }); const closeDialog = () => { + capabilityRequestSequence += 1; dialogVisible.value = false; }; +const detectModelCapabilities = async () => { + const modelName = formData.modelName.trim(); + if (!modelName || manuallyEditedModelName === modelName) { + return; + } + + const requestSequence = ++capabilityRequestSequence; + capabilityLoading.value = true; + try { + const providerId = isAdd.value + ? selectedProviderId.value + : formData.providerId; + const res = await resolveModelCapabilities({ modelName, providerId }); + if ( + requestSequence !== capabilityRequestSequence || + res.errorCode !== 0 || + !res.data + ) { + return; + } + + const capability = res.data; + manuallyEditedModelName = ''; + if (!capability.detected) { + if (autoDetectedModelType.value) { + selectedModelType.value = ''; + } + autoDetectedModelType.value = false; + formData.supportThinking = null; + formData.supportTool = null; + formData.supportImage = null; + formData.supportToolMessage = null; + syncTagSelectedStatus(); + return; + } + + autoDetectedModelType.value = true; + formData.modelType = capability.modelType; + selectedModelType.value = normalizeSelectableModelType( + capability.modelType, + ); + formData.supportThinking = capability.supportThinking ?? null; + formData.supportTool = capability.supportTool ?? null; + formData.supportImage = capability.supportImage ?? null; + formData.supportToolMessage = capability.supportTool ?? null; + syncTagSelectedStatus(); + } catch { + // 自动识别失败不阻塞表单,保存时后端仍会再次解析能力。 + } finally { + if (requestSequence === capabilityRequestSequence) { + capabilityLoading.value = false; + } + } +}; + const rules = { title: [{ required: true, message: $t('message.required'), trigger: 'blur' }], modelName: [ @@ -323,19 +414,11 @@ const rules = { const save = async () => { btnLoading.value = true; const modelType = resolveModelType(); - const features = generateFeaturesFromModelAbility(modelAbility.value); - - if (modelType !== 'chatModel') { - for (const key of Object.keys(features) as Array) { - features[key] = false; - } - } try { await formDataRef.value.validate(); const submitData = { ...formData, - ...features, modelType, providerId: isAdd.value ? selectedProviderId.value : formData.providerId, }; @@ -393,6 +476,8 @@ const save = async () => { @@ -402,21 +487,40 @@ const save = async () => { /> - -
+ + +
+ + +
- - + + +
+ + + + + + + + + + +
+
@@ -511,6 +640,33 @@ const save = async () => { margin-top: 4px; } +.model-modal__ability-label { + display: inline-flex; + gap: var(--space-1); + align-items: center; +} + +.model-modal__ability-info { + display: inline-flex; + padding: 0; + color: hsl(var(--text-muted)); + cursor: help; + background: transparent; + border: 0; + border-radius: var(--radius-control); + transition: color var(--motion-duration-base) var(--motion-ease-standard); +} + +.model-modal__ability-info:hover, +.model-modal__ability-info:focus-visible { + color: hsl(var(--primary)); +} + +.model-modal__ability-info:focus-visible { + outline: 2px solid hsl(var(--primary) / 24%); + outline-offset: 2px; +} + .model-modal__ability-panel { padding: 2px; overflow: hidden; @@ -543,7 +699,7 @@ const save = async () => { font-weight: 600; line-height: 1; color: hsl(var(--text-muted)); - cursor: pointer; + cursor: default; background: hsl(var(--surface-contrast-soft) / 86%); border: 1px solid transparent; border-radius: 999px; @@ -555,8 +711,12 @@ const save = async () => { box-shadow 0.2s ease; } -.model-modal__ability-chip:hover:not(:disabled), -.model-modal__ability-chip:focus-visible:not(:disabled) { +.model-modal__ability-chip.is-interactive { + cursor: pointer; +} + +.model-modal__ability-chip.is-interactive:hover, +.model-modal__ability-chip.is-interactive:focus-visible { color: hsl(var(--text-strong)); box-shadow: 0 10px 18px -14px hsl(var(--foreground) / 28%); transform: translateY(-1px); @@ -570,7 +730,7 @@ const save = async () => { } .model-modal__ability-chip.is-disabled { - cursor: not-allowed; + cursor: default; box-shadow: none; opacity: 0.56; transform: none; @@ -581,9 +741,13 @@ const save = async () => { opacity: 0.88; } +.model-modal__ability-loading { + margin-inline-start: var(--space-1); + color: hsl(var(--text-muted)); +} + .model-modal__ability-chip.is-active.is-tone-embeddingModel, -.model-modal__ability-chip.is-active.is-tone-thinking, -.model-modal__ability-chip.is-active.is-tone-toolMessage { +.model-modal__ability-chip.is-active.is-tone-thinking { color: hsl(var(--primary)); background: hsl(var(--primary) / 10%); border-color: hsl(var(--primary) / 18%); @@ -598,21 +762,56 @@ const save = async () => { box-shadow: inset 0 0 0 1px hsl(var(--warning) / 14%); } -.model-modal__ability-chip.is-active.is-tone-image, -.model-modal__ability-chip.is-active.is-tone-imageB64 { +.model-modal__ability-chip.is-active.is-tone-image { color: hsl(var(--success)); background: hsl(var(--success) / 12%); border-color: hsl(var(--success) / 18%); box-shadow: inset 0 0 0 1px hsl(var(--success) / 14%); } -.model-modal__ability-chip.is-active.is-tone-audio, -.model-modal__ability-chip.is-active.is-tone-video, -.model-modal__ability-chip.is-active.is-tone-free { - color: hsl(var(--danger)); - background: hsl(var(--danger) / 10%); - border-color: hsl(var(--danger) / 16%); - box-shadow: inset 0 0 0 1px hsl(var(--danger) / 12%); +.model-modal__advanced { + display: flex; + flex-direction: column; + gap: var(--space-3); +} + +.model-modal__advanced-toggle { + display: flex; + align-items: center; + justify-content: space-between; + width: 100%; + padding: var(--space-2) var(--space-3); + font-size: 13px; + color: hsl(var(--text-muted)); + cursor: pointer; + background: hsl(var(--surface-contrast-soft) / 68%); + border: 1px solid hsl(var(--divider-faint) / 56%); + border-radius: var(--radius-control); + transition: + color var(--motion-duration-base) var(--motion-ease-standard), + border-color var(--motion-duration-base) var(--motion-ease-standard), + background var(--motion-duration-base) var(--motion-ease-standard); +} + +.model-modal__advanced-toggle:hover, +.model-modal__advanced-toggle:focus-visible { + color: hsl(var(--text-strong)); + background: hsl(var(--surface-contrast-soft)); + border-color: hsl(var(--divider-faint)); +} + +.model-modal__advanced-body :deep(.el-form-item) { + margin-bottom: 0; +} + +.model-modal__advanced-body { + display: flex; + flex-direction: column; + gap: var(--space-3); +} + +.model-modal__advanced-body :deep(.el-select) { + width: 100%; } @media (max-width: 640px) { diff --git a/easyflow-ui-admin/app/src/views/ai/model/ModelVerifyConfig.vue b/easyflow-ui-admin/app/src/views/ai/model/ModelVerifyConfig.vue index f0ba8cb8..b2e70209 100644 --- a/easyflow-ui-admin/app/src/views/ai/model/ModelVerifyConfig.vue +++ b/easyflow-ui-admin/app/src/views/ai/model/ModelVerifyConfig.vue @@ -33,7 +33,7 @@ const formData = reactive({ const resultTitle = computed(() => { if (verifyStatus.value === 'success') { - return '验证成功'; + return '验证通过'; } if (verifyStatus.value === 'error') { @@ -41,7 +41,7 @@ const resultTitle = computed(() => { } if (verifyStatus.value === 'warning') { - return '流式不可用'; + return '验证通过'; } return '等待验证'; @@ -145,7 +145,7 @@ const save = async () => {

1. 选择待验证模型

-

会用当前保存的配置检查基础连接和流式响应。

+

使用当前保存的配置验证模型。

{ const res = await verifyModelConfig(modelId); const feedback = resolveModelVerificationFeedback(res, llm.modelType); + if (typeof res.data?.supportTool === 'boolean') { + llm.supportTool = res.data.supportTool; + } setVerifyStatus(modelId, feedback.status); if (feedback.status === 'success') { diff --git a/easyflow-ui-admin/app/src/views/ai/model/UnifiedGatewayWorkspace.vue b/easyflow-ui-admin/app/src/views/ai/model/UnifiedGatewayWorkspace.vue index 4382c51c..03ff11ea 100644 --- a/easyflow-ui-admin/app/src/views/ai/model/UnifiedGatewayWorkspace.vue +++ b/easyflow-ui-admin/app/src/views/ai/model/UnifiedGatewayWorkspace.vue @@ -184,19 +184,15 @@ const upstreamModelName = computed( ); const capabilityTags = computed(() => { - const tags = ['文本', '流式']; + const tags = ['文本']; + if (selectedModel.value?.supportThinking) { + tags.push('推理'); + } if (selectedModel.value?.supportImage) { - tags.push( - selectedModel.value?.supportImageB64Only - ? '图片输入(Base64)' - : '图片输入', - ); + tags.push('视觉'); } if (selectedModel.value?.supportTool) { - tags.push('tools'); - } - if (selectedModel.value?.supportToolMessage) { - tags.push('tool 消息'); + tags.push('工具'); } return tags; }); diff --git a/easyflow-ui-admin/app/src/views/ai/model/modelUtils/__tests__/model-ability.test.ts b/easyflow-ui-admin/app/src/views/ai/model/modelUtils/__tests__/model-ability.test.ts new file mode 100644 index 00000000..b2eac681 --- /dev/null +++ b/easyflow-ui-admin/app/src/views/ai/model/modelUtils/__tests__/model-ability.test.ts @@ -0,0 +1,35 @@ +import { describe, expect, it } from 'vitest'; + +import { getDefaultModelAbility, handleTagClick } from '../model-ability'; + +describe('model ability labels', () => { + it('只保留当前可用的视觉、推理和工具能力', () => { + const abilities = getDefaultModelAbility(); + + expect(abilities.map((item) => item.field)).toEqual([ + 'supportThinking', + 'supportTool', + 'supportImage', + ]); + }); + + it('支持手动切换自动识别的能力', () => { + const abilities = getDefaultModelAbility(); + const toolAbility = abilities.find((item) => item.field === 'supportTool'); + const formData = { + supportImage: false, + supportThinking: false, + supportTool: false, + }; + + expect(toolAbility).toBeDefined(); + if (!toolAbility) { + throw new Error('缺少工具能力标签'); + } + + handleTagClick(toolAbility, formData); + + expect(toolAbility.selected).toBe(true); + expect(formData.supportTool).toBe(true); + }); +}); diff --git a/easyflow-ui-admin/app/src/views/ai/model/modelUtils/__tests__/model-verification.test.ts b/easyflow-ui-admin/app/src/views/ai/model/modelUtils/__tests__/model-verification.test.ts index 56289106..a91d22ed 100644 --- a/easyflow-ui-admin/app/src/views/ai/model/modelUtils/__tests__/model-verification.test.ts +++ b/easyflow-ui-admin/app/src/views/ai/model/modelUtils/__tests__/model-verification.test.ts @@ -6,7 +6,7 @@ import { } from '../model-verification'; describe('model verification helpers', () => { - it('双阶段通过时返回成功状态', () => { + it('验证通过时只返回统一成功文案', () => { expect( resolveModelVerificationFeedback( { @@ -17,12 +17,12 @@ describe('model verification helpers', () => { ), ).toEqual({ dimension: undefined, - message: '验证成功', + message: '验证通过', status: 'success', }); }); - it('基础连接通过但流式失败时返回警告状态', () => { + it('旧版部分通过结果也收敛为统一成功文案', () => { const feedback = resolveModelVerificationFeedback( { data: { @@ -34,9 +34,9 @@ describe('model verification helpers', () => { 'chatModel', ); - expect(feedback.status).toBe('warning'); - expect(feedback.message).toContain('流式响应不可用'); - expect(getVerifyButtonText('warning')).toBe('流式不可用'); + expect(feedback.status).toBe('success'); + expect(feedback.message).toBe('验证通过'); + expect(getVerifyButtonText('warning')).toBe('验证通过'); }); it('向量模型验证保留维度结果', () => { @@ -47,7 +47,7 @@ describe('model verification helpers', () => { ), ).toEqual({ dimension: 1024, - message: '验证成功,向量维度:1024', + message: '验证通过', status: 'success', }); }); diff --git a/easyflow-ui-admin/app/src/views/ai/model/modelUtils/model-ability.ts b/easyflow-ui-admin/app/src/views/ai/model/modelUtils/model-ability.ts index 328ed941..6c5fb758 100644 --- a/easyflow-ui-admin/app/src/views/ai/model/modelUtils/model-ability.ts +++ b/easyflow-ui-admin/app/src/views/ai/model/modelUtils/model-ability.ts @@ -1,13 +1,6 @@ import { $t } from '#/locales'; -export type BooleanField = - | 'supportAudio' - | 'supportImage' - | 'supportImageB64Only' - | 'supportThinking' - | 'supportTool' - | 'supportToolMessage' - | 'supportVideo'; +export type BooleanField = 'supportImage' | 'supportThinking' | 'supportTool'; export interface ModelAbilityItem { activeType: 'danger' | 'info' | 'primary' | 'success' | 'warning'; @@ -39,14 +32,6 @@ export const getDefaultModelAbility = (): ModelAbilityItem[] => [ selected: false, field: 'supportTool', }, - { - label: $t('llm.modelAbility.supportVideo'), - value: 'video', - defaultType: 'info', - activeType: 'success', - selected: false, - field: 'supportVideo', - }, { label: $t('llm.modelAbility.supportImage'), value: 'image', @@ -55,30 +40,6 @@ export const getDefaultModelAbility = (): ModelAbilityItem[] => [ selected: false, field: 'supportImage', }, - { - label: $t('llm.modelAbility.supportAudio'), - value: 'audio', - defaultType: 'info', - activeType: 'success', - selected: false, - field: 'supportAudio', - }, - { - label: $t('llm.modelAbility.supportImageB64Only'), - value: 'imageB64', - defaultType: 'info', - activeType: 'success', - selected: false, - field: 'supportImageB64Only', - }, - { - label: $t('llm.modelAbility.supportToolMessage'), - value: 'toolMessage', - defaultType: 'info', - activeType: 'success', - selected: true, - field: 'supportToolMessage', - }, ]; /** @@ -108,7 +69,7 @@ export const getTagsSelectedStatus = ( */ export const syncTagSelectedStatus = ( modelAbility: ModelAbilityItem[], - formData: Record, + formData: Record, ): void => { modelAbility.forEach((tag) => { tag.selected = formData[tag.field] ?? false; @@ -121,9 +82,8 @@ export const syncTagSelectedStatus = ( * @param formData 表单数据对象 */ export const handleTagClick = ( - // modelAbility: ModelAbilityItem[], item: ModelAbilityItem, - formData: Record, + formData: Record, ): void => { // 切换标签选中状态 item.selected = !item.selected; @@ -152,7 +112,4 @@ export const getAllBooleanFields = (): BooleanField[] => [ 'supportThinking', 'supportTool', 'supportImage', - 'supportImageB64Only', - 'supportVideo', - 'supportAudio', ]; diff --git a/easyflow-ui-admin/app/src/views/ai/model/modelUtils/model-verification.ts b/easyflow-ui-admin/app/src/views/ai/model/modelUtils/model-verification.ts index 68085227..96f52785 100644 --- a/easyflow-ui-admin/app/src/views/ai/model/modelUtils/model-verification.ts +++ b/easyflow-ui-admin/app/src/views/ai/model/modelUtils/model-verification.ts @@ -19,12 +19,9 @@ interface ModelVerificationResponse { message?: string; } -const STREAMING_UNAVAILABLE_MESSAGE = - '连接成功,流式响应不可用,可关闭智能体的模型流式响应。'; - export function resolveModelVerificationFeedback( response: ModelVerificationResponse, - modelType: string, + _modelType: string, ): ModelVerificationFeedback { if (response.errorCode !== 0) { return { @@ -33,13 +30,6 @@ export function resolveModelVerificationFeedback( }; } - if (response.data?.status === 'PARTIAL') { - return { - message: response.data.message || STREAMING_UNAVAILABLE_MESSAGE, - status: 'warning', - }; - } - if (response.data?.status === 'FAILED') { return { message: response.data.message || '验证失败', @@ -50,10 +40,7 @@ export function resolveModelVerificationFeedback( const dimension = response.data?.dimension; return { dimension, - message: - modelType === 'embeddingModel' && dimension - ? `验证成功,向量维度:${dimension}` - : response.data?.message || '验证成功', + message: '验证通过', status: 'success', }; } @@ -63,10 +50,10 @@ export function getVerifyButtonText(status: VerifyButtonStatus): string { return '验证中'; } if (status === 'success') { - return '验证成功'; + return '验证通过'; } if (status === 'warning') { - return '流式不可用'; + return '验证通过'; } if (status === 'error') { return '验证失败';