Skip to content

Implement AI API connection test endpoint#294

Merged
239573049 merged 2 commits intomainfrom
copilot/add-api-connection-test
Nov 13, 2025
Merged

Implement AI API connection test endpoint#294
239573049 merged 2 commits intomainfrom
copilot/add-api-connection-test

Conversation

Copy link
Contributor

Copilot AI commented Nov 13, 2025

System settings UI provides a "测试API连接" button but the backend endpoint /api/SystemSetting/test/ai was missing.

Changes

Added DTOs (src/KoalaWiki/Dto/SystemSettingDto.cs):

  • TestAIApiRequest - accepts endpoint, apiKey, optional model
  • TestResultResponse - returns success/failure with message and details

Implemented endpoint (src/KoalaWiki/Services/SystemSettingService.cs):

  • POST /api/SystemSetting/test/ai - validates AI credentials by creating ephemeral Kernel and sending test prompt
  • Uses existing KernelFactory.GetKernel() with user-provided credentials
  • Returns structured response with error details on failure
  • Cleans up temporary resources in finally block

Frontend integration already exists at AISettingsTab.tsx:74-118 and admin.service.ts:677-684.

[HttpPost("/test/ai")]
public async Task<TestResultResponse> TestAIConnectionAsync([FromBody] TestAIApiRequest request)
{
    var kernel = await KernelFactory.GetKernel(
        chatEndpoint: request.Endpoint,
        apiKey: request.ApiKey,
        model: request.Model ?? OpenAIOptions.ChatModel,
        isCodeAnalysis: false
    );
    
    var response = await kernel
        .GetRequiredService<IChatCompletionService>()
        .GetChatMessageContentAsync(new ChatHistory { "Hello" });
    
    return new TestResultResponse { 
        Success = true, 
        Message = "AI API连接测试成功,模型响应正常" 
    };
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • nuget.cdn.azure.cn
    • Triggering command: dotnet build KoalaWiki.sln (dns block)
    • Triggering command: dotnet restore (dns block)
    • Triggering command: dotnet build (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>完善测试 API 连接功能</issue_title>
<issue_description>补充 /api/SystemSetting/test/ai 接口实现

Image</issue_description>

<agent_instructions>在系统配置界面当中提供了测试API的功能,用于测试配置的AI是否可用,需要提供API进行实现接入给前端</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: 239573049 <61819790+239573049@users.noreply.github.com>
Copilot AI changed the title [WIP] Add implementation for testing API connection functionality Implement AI API connection test endpoint Nov 13, 2025
Copilot AI requested a review from 239573049 November 13, 2025 05:55
@239573049 239573049 marked this pull request as ready for review November 13, 2025 05:55
Copilot AI review requested due to automatic review settings November 13, 2025 05:55
@239573049 239573049 merged commit ab25998 into main Nov 13, 2025
2 checks passed
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements the missing backend endpoint for testing AI API connections, addressing a gap where the frontend "测试API连接" button had no corresponding backend implementation.

Key changes:

  • Added TestAIApiRequest and TestResultResponse DTOs for structured request/response handling
  • Implemented POST /api/SystemSetting/test/ai endpoint that validates AI credentials by creating an ephemeral Kernel and sending a test prompt
  • Includes comprehensive input validation, error handling, and temporary resource cleanup

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
src/KoalaWiki/Dto/SystemSettingDto.cs Added DTOs for AI API test request (endpoint, apiKey, model) and response (success, message, details)
src/KoalaWiki/Services/SystemSettingService.cs Implemented test endpoint with validation, Kernel creation, test prompt execution, and cleanup logic

gitPath: tempPath,
model: model,
isCodeAnalysis: false, // 测试时不需要加载代码分析插件
files: null
Copy link

Copilot AI Nov 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The KernelFactory.GetKernel method uses the global OpenAIOptions.ModelProvider setting to determine whether to use OpenAI or AzureOpenAI. This means the test endpoint cannot validate different AI providers - it will always test using the currently configured provider in the system settings.

Recommendation: Add a ModelProvider parameter to TestAIApiRequest DTO and pass it through to the kernel creation, or implement a separate test method that doesn't rely on global configuration. This would allow administrators to test different provider configurations before saving them.

Suggested change
files: null
files: null,
modelProvider: request.ModelProvider // 新增参数,允许测试不同AI Provider

Copilot uses AI. Check for mistakes.
};
}

// 创建临时目录用于测试
Copy link

Copilot AI Nov 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The temporary directory is created before validating the model parameter. If the model validation fails (line 320-327), the directory will be left behind and never cleaned up since the cleanup code is in a nested finally block.

Recommendation: Move the temporary directory creation after all parameter validations have passed (after line 327) to prevent resource leaks.

Suggested change
// 创建临时目录用于测试
// 创建临时目录用于测试(已通过所有参数校验后再创建)

Copilot uses AI. Check for mistakes.
{
Model = model,
Endpoint = request.Endpoint,
Response = response.Content
Copy link

Copilot AI Nov 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Exposing the AI model's response content in the API response could potentially leak sensitive information if the model's response contains unexpected content or reflects back test data in unintended ways.

Recommendation: Consider either:

  1. Omitting response.Content from the Details and just confirming the response was received
  2. Truncating the response to a safe length (e.g., first 100 characters)
  3. Only including a hash or confirmation that a non-empty response was received
Suggested change
Response = response.Content
ResponsePreview = response.Content != null && response.Content.Length > 100
? response.Content.Substring(0, 100) + "..."
: response.Content

Copilot uses AI. Check for mistakes.
Comment on lines +336 to +364
var kernel = await KernelFactory.GetKernel(
chatEndpoint: request.Endpoint,
apiKey: request.ApiKey,
gitPath: tempPath,
model: model,
isCodeAnalysis: false, // 测试时不需要加载代码分析插件
files: null
);

// 发送测试消息
var chatService = kernel.GetRequiredService<IChatCompletionService>();
var chatHistory = new ChatHistory();
chatHistory.AddUserMessage("Hello");

var response = await chatService.GetChatMessageContentAsync(chatHistory);

logger.LogInformation("AI API连接测试成功");

return new TestResultResponse
{
Success = true,
Message = "AI API连接测试成功,模型响应正常",
Details = new
{
Model = model,
Endpoint = request.Endpoint,
Response = response.Content
}
};
Copy link

Copilot AI Nov 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The kernel instance created for testing is not explicitly disposed. While this follows the existing pattern in the codebase (e.g., ResponsesService.cs line 87-88), the kernel contains HttpClient instances created in KernelFactory.GetKernel.

Recommendation: Consider wrapping the kernel usage in a using statement if Kernel implements IDisposable, or investigate whether the Semantic Kernel framework handles disposal internally. This would ensure proper cleanup of HTTP connections, especially important for a test endpoint that may be called frequently.

Copilot uses AI. Check for mistakes.
gitPath: tempPath,
model: model,
isCodeAnalysis: false, // 测试时不需要加载代码分析插件
files: null
Copy link

Copilot AI Nov 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The test endpoint passes isCodeAnalysis: false which correctly skips loading code analysis plugins. However, KernelFactory.GetKernel still creates FileTool, AgentTool, and attempts to load MCP services (lines 80-104 in KernelFactory.cs). These plugins and tools are unnecessary for a simple API connection test and add overhead.

Recommendation: For a lighter-weight test, consider either:

  1. Creating a simplified kernel initialization method specifically for testing that skips all plugin loading
  2. Using the kernelBuilderAction parameter to customize kernel creation without unnecessary plugins
  3. Documenting that the test validates the full kernel initialization pipeline, which may be intentional
Suggested change
files: null
files: null,
kernelBuilderAction: builder => { /* no-op: skip plugin/tool registration for test */ }

Copilot uses AI. Check for mistakes.
@239573049 239573049 deleted the copilot/add-api-connection-test branch January 30, 2026 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

完善测试 API 连接功能

3 participants