API 测试功能诊断'; // 1. 检查函数是否存在 echo '

1. 函数存在性检查

'; $functions = [ 'argon_ajax_test_unified_api', 'argon_get_api_by_id', 'argon_get_all_apis' ]; foreach ($functions as $func) { if (function_exists($func)) { echo '

✓ ' . $func . ' 存在

'; } else { echo '

✗ ' . $func . ' 不存在

'; } } // 2. 检查是否有 API 配置 echo '

2. API 配置检查

'; $all_apis = argon_get_all_apis(); if (empty($all_apis)) { echo '

⚠ 没有配置任何 API

'; } else { echo '

✓ 已配置 ' . count($all_apis) . ' 个 API

'; foreach ($all_apis as $api) { echo '
'; echo '' . esc_html($api['name']) . '
'; echo 'ID: ' . esc_html($api['id']) . '
'; echo '提供商: ' . esc_html($api['provider']) . '
'; echo '模型: ' . esc_html($api['model']) . '
'; echo '
'; } } // 3. 检查 AJAX action 是否注册 echo '

3. AJAX Action 注册检查

'; global $wp_filter; if (isset($wp_filter['wp_ajax_argon_ajax_test_unified_api'])) { echo '

✓ wp_ajax_argon_ajax_test_unified_api 已注册

'; } else { echo '

✗ wp_ajax_argon_ajax_test_unified_api 未注册

'; } // 4. 测试 nonce 生成 echo '

4. Nonce 生成测试

'; $nonce = wp_create_nonce('argon_test_unified_api'); echo '

生成的 nonce: ' . $nonce . '

'; echo '

验证结果: '; if (wp_verify_nonce($nonce, 'argon_test_unified_api')) { echo '✓ 验证通过

'; } else { echo '✗ 验证失败

'; } // 5. 模拟 AJAX 请求测试 if (!empty($all_apis)) { echo '

5. 模拟 AJAX 请求测试

'; $test_api = $all_apis[0]; echo '

测试 API: ' . esc_html($test_api['name']) . '

'; // 模拟 $_POST 数据 $_POST['api_id'] = $test_api['id']; $_POST['nonce'] = wp_create_nonce('argon_test_unified_api'); echo '

开始测试...

'; // 获取 API 配置 $api = argon_get_api_by_id($test_api['id']); if (!$api) { echo '

✗ 无法获取 API 配置

'; } else { echo '

✓ 成功获取 API 配置

'; // 获取默认端点 $default_endpoints = [ 'openai' => 'https://api.openai.com/v1/chat/completions', 'anthropic' => 'https://api.anthropic.com/v1/messages', 'deepseek' => 'https://api.deepseek.com/v1/chat/completions', 'xiaomi' => 'https://api.mimo.xiaomi.com/v1/chat/completions', 'qianwen' => 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions', 'wenxin' => 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions', 'doubao' => 'https://ark.cn-beijing.volces.com/api/v3/chat/completions', 'kimi' => 'https://api.moonshot.cn/v1/chat/completions', 'zhipu' => 'https://open.bigmodel.cn/api/paas/v4/chat/completions', 'siliconflow' => 'https://api.siliconflow.cn/v1/chat/completions' ]; $api_endpoint = !empty($api['api_endpoint']) ? $api['api_endpoint'] : $default_endpoints[$api['provider']]; $model = !empty($api['model']) ? $api['model'] : 'gpt-4o-mini'; echo '

API 端点: ' . esc_html($api_endpoint) . '

'; echo '

模型: ' . esc_html($model) . '

'; // 构建测试请求 $data = [ 'model' => $model, 'messages' => [ [ 'role' => 'user', 'content' => '你好,这是一个测试。请回复"测试成功"。' ] ], 'max_tokens' => 50 ]; echo '

发送测试请求...

'; $start_time = microtime(true); $response = wp_remote_post($api_endpoint, [ 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $api['api_key'] ], 'body' => json_encode($data), 'timeout' => 30 ]); $response_time = round((microtime(true) - $start_time) * 1000); if (is_wp_error($response)) { echo '

✗ 请求失败: ' . $response->get_error_message() . '

'; } else { $status_code = wp_remote_retrieve_response_code($response); $body = wp_remote_retrieve_body($response); echo '

HTTP 状态码: ' . $status_code . '

'; echo '

响应时间: ' . $response_time . 'ms

'; if ($status_code === 200) { $result = json_decode($body, true); if (isset($result['choices'][0]['message']['content']) || isset($result['content'])) { echo '

✓ API 测试成功!

'; if (isset($result['choices'][0]['message']['content'])) { echo '

AI 回复: ' . esc_html($result['choices'][0]['message']['content']) . '

'; } } else { echo '

✗ API 返回格式异常

'; echo '
' . htmlspecialchars($body) . '
'; } } else { echo '

✗ API 返回错误

'; echo '
' . htmlspecialchars($body) . '
'; } } } } echo '
'; echo '

测试完成

'; echo '

返回设置页

'; ?>