修复AI工具调用报错

This commit is contained in:
2025-09-17 18:29:54 +08:00
parent dd3f702887
commit 5613cdd6c9
22 changed files with 260 additions and 282 deletions

View File

@@ -3,7 +3,7 @@
// API配置
window.API_CONFIG = {
baseUrl: 'http://127.0.0.1:5002',
baseUrl: window.parent.ENV_CONFIG.API_URL,
endpoints: {
poetry: '/api/aimodelapp/poetry'
}

View File

@@ -10,18 +10,19 @@ const themeInput = document.getElementById('theme');
// 调用后端API
async function callBackendAPI(theme) {
try {
const token = AUTH_CONFIG.getToken();
const headers = {
'Content-Type': 'application/json'
};
// 获取JWT token
const token = localStorage.getItem('token');
if (token) {
headers['Authorization'] = `Bearer ${token}`;
if (!token) {
throw new Error('未登录请先登录后使用AI功能');
}
const response = await fetch(`${API_CONFIG.baseUrl}${API_CONFIG.endpoints.poetry}`, {
const response = await fetch(`${window.API_CONFIG.baseUrl}/api/aimodelapp/poetry`, {
method: 'POST',
headers: headers,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
theme: theme
})

View File

@@ -3,7 +3,7 @@
// API配置
window.API_CONFIG = {
baseUrl: 'http://127.0.0.1:5002',
baseUrl: window.parent.ENV_CONFIG.API_URL,
endpoints: {
variableNaming: '/api/aimodelapp/variable-naming'
}

View File

@@ -47,7 +47,7 @@ async function callBackendAPI(description) {
throw new Error('未登录请先登录后使用AI功能');
}
const response = await fetch('http://127.0.0.1:5002/api/aimodelapp/variable-naming', {
const response = await fetch(`${window.API_CONFIG.baseUrl}/api/aimodelapp/variable-naming`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',

View File

@@ -3,7 +3,7 @@
// API配置
window.API_CONFIG = {
baseUrl: 'http://127.0.0.1:5002',
baseUrl: window.parent.ENV_CONFIG.API_URL,
endpoints: {
nameAnalysis: '/api/aimodelapp/name-analysis'
}

View File

@@ -193,18 +193,19 @@ async function analyzeName() {
try {
// 调用后端API
const token = AUTH_CONFIG.getToken();
const headers = {
'Content-Type': 'application/json'
};
// 获取JWT token
const token = localStorage.getItem('token');
if (token) {
headers['Authorization'] = `Bearer ${token}`;
if (!token) {
throw new Error('未登录请先登录后使用AI功能');
}
const response = await fetch(`${API_CONFIG.baseUrl}${API_CONFIG.endpoints.nameAnalysis}`, {
const response = await fetch(`${window.API_CONFIG.baseUrl}/api/aimodelapp/name-analysis`, {
method: 'POST',
headers: headers,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(requestBody)
});

View File

@@ -3,7 +3,7 @@
// API配置
window.API_CONFIG = {
baseUrl: 'http://127.0.0.1:5002',
baseUrl: window.parent.ENV_CONFIG.API_URL,
endpoints: {
classicalConversion: '/api/aimodelapp/classical_conversion'
}

View File

@@ -12,18 +12,19 @@ const conversionResultContainer = document.getElementById('conversionResult');
// 调用后端API
async function callBackendAPI(modernText, style, articleType) {
try {
const token = AUTH_CONFIG.getToken();
const headers = {
'Content-Type': 'application/json'
};
// 获取JWT token
const token = localStorage.getItem('token');
if (token) {
headers['Authorization'] = `Bearer ${token}`;
if (!token) {
throw new Error('未登录请先登录后使用AI功能');
}
const response = await fetch(`${API_CONFIG.baseUrl}${API_CONFIG.endpoints.classicalConversion}`, {
const response = await fetch(`${window.API_CONFIG.baseUrl}/api/aimodelapp/classical_conversion`, {
method: 'POST',
headers: headers,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
modern_text: modernText,
style: style,

View File

@@ -3,7 +3,7 @@
// API配置
window.API_CONFIG = {
baseUrl: 'http://127.0.0.1:5002',
baseUrl: window.parent.ENV_CONFIG.API_URL,
endpoints: {
linuxCommand: '/api/aimodelapp/linux-command'
}

View File

@@ -11,7 +11,7 @@ const commandsContainer = document.getElementById('commands');
// 调用后端API
async function callBackendAPI(taskDescription, difficultyLevel) {
try {
const token = AUTH_CONFIG.getToken();
const token = localStorage.getItem('token');
const headers = {
'Content-Type': 'application/json'
};
@@ -20,7 +20,7 @@ async function callBackendAPI(taskDescription, difficultyLevel) {
headers['Authorization'] = `Bearer ${token}`;
}
const response = await fetch(`${API_CONFIG.baseUrl}${API_CONFIG.endpoints.linuxCommand}`, {
const response = await fetch(`${window.API_CONFIG.baseUrl}${window.API_CONFIG.endpoints.linuxCommand}`, {
method: 'POST',
headers: headers,
body: JSON.stringify({

View File

@@ -3,7 +3,7 @@
// API配置
window.API_CONFIG = {
baseUrl: 'http://127.0.0.1:5002',
baseUrl: window.parent.ENV_CONFIG.API_URL,
endpoints: {
expressionMaker: '/api/aimodelapp/expression-maker'
}

View File

@@ -1,6 +1,29 @@
// 从配置文件导入设置
// 配置在 env.js 文件中定义
// 表情配置
const CONFIG = {
intensityLevels: {
'low': { color: '#4CAF50' },
'medium': { color: '#FF9800' },
'high': { color: '#F44336' },
'very_high': { color: '#9C27B0' }
},
expressionCategories: {
'emoji': { className: 'emoji' },
'kaomoji': { className: 'kaomoji' },
'combination': { className: 'combination' }
},
expressionStyles: {
'cute': { name: '可爱风', description: '适合表达可爱、萌系情感' },
'cool': { name: '酷炫风', description: '适合表达酷炫、帅气情感' },
'angry': { name: '愤怒风', description: '适合表达愤怒、生气情感' },
'sad': { name: '悲伤风', description: '适合表达悲伤、难过情感' },
'happy': { name: '开心风', description: '适合表达开心、快乐情感' },
'mixed': { name: '混合风', description: '多种风格混合,适应各种情感' }
}
};
// DOM 元素
const textInput = document.getElementById('text-input');
const styleSelect = document.getElementById('style-select');
@@ -18,7 +41,7 @@ async function callBackendAPI(text, style) {
throw new Error('未登录请先登录后使用AI功能');
}
const response = await fetch(`${API_CONFIG.baseUrl}${API_CONFIG.endpoints.expressionMaker}`, {
const response = await fetch(`${window.API_CONFIG.baseUrl}/api/aimodelapp/expression-maker`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',

View File

@@ -3,7 +3,7 @@
// API配置
window.API_CONFIG = {
baseUrl: 'http://127.0.0.1:5002',
baseUrl: window.parent.ENV_CONFIG.API_URL,
endpoints: {
translation: '/api/aimodelapp/translation'
}

View File

@@ -11,18 +11,19 @@ const translationResultContainer = document.getElementById('translationResult');
// 调用后端API
async function callBackendAPI(sourceText, targetLanguage) {
try {
const token = AUTH_CONFIG.getToken();
const headers = {
'Content-Type': 'application/json'
};
// 获取JWT token
const token = localStorage.getItem('token');
if (token) {
headers['Authorization'] = `Bearer ${token}`;
if (!token) {
throw new Error('未登录请先登录后使用AI功能');
}
const response = await fetch(`${API_CONFIG.baseUrl}${API_CONFIG.endpoints.translation}`, {
const response = await fetch(`${window.API_CONFIG.baseUrl}/api/aimodelapp/translation`, {
method: 'POST',
headers: headers,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
source_text: sourceText,
target_language: targetLanguage

View File

@@ -1,228 +0,0 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API超时修复测试</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: #f5f5f7;
}
.container {
background: white;
border-radius: 12px;
padding: 30px;
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}
h1 {
color: #1d1d1f;
text-align: center;
margin-bottom: 30px;
}
.test-section {
margin-bottom: 30px;
padding: 20px;
border: 1px solid #e5e5e7;
border-radius: 8px;
}
.test-section h3 {
color: #333;
margin-top: 0;
}
input, select, button {
width: 100%;
padding: 12px;
margin: 8px 0;
border: 1px solid #d2d2d7;
border-radius: 8px;
font-size: 16px;
}
button {
background: #007aff;
color: white;
border: none;
cursor: pointer;
transition: background 0.3s;
}
button:hover {
background: #0056b3;
}
button:disabled {
background: #ccc;
cursor: not-allowed;
}
.result {
margin-top: 15px;
padding: 15px;
border-radius: 8px;
white-space: pre-wrap;
font-family: monospace;
font-size: 14px;
}
.success {
background: #d4edda;
border: 1px solid #c3e6cb;
color: #155724;
}
.error {
background: #f8d7da;
border: 1px solid #f5c6cb;
color: #721c24;
}
.loading {
background: #fff3cd;
border: 1px solid #ffeaa7;
color: #856404;
}
.status {
font-weight: bold;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>🔧 API超时修复测试</h1>
<div class="test-section">
<h3>🎭 表情制作器API测试</h3>
<input type="text" id="expressionText" placeholder="输入要生成表情的文字" value="开心">
<select id="expressionStyle">
<option value="mixed">混合风格</option>
<option value="emoji">仅Emoji</option>
<option value="kaomoji">仅颜文字</option>
<option value="cute">可爱风格</option>
<option value="cool">酷炫风格</option>
</select>
<button onclick="testExpressionAPI()">测试表情API</button>
<div id="expressionResult"></div>
</div>
<div class="test-section">
<h3>📝 变量命名API测试</h3>
<input type="text" id="variableDesc" placeholder="输入变量描述" value="用户的年龄">
<button onclick="testVariableAPI()">测试变量命名API</button>
<div id="variableResult"></div>
</div>
</div>
<script>
async function testExpressionAPI() {
const text = document.getElementById('expressionText').value.trim();
const style = document.getElementById('expressionStyle').value;
const resultDiv = document.getElementById('expressionResult');
if (!text) {
showResult(resultDiv, '请输入文字内容', 'error');
return;
}
showResult(resultDiv, '正在调用表情制作API...\n请耐心等待最多90秒', 'loading');
const startTime = Date.now();
try {
const response = await fetch('http://127.0.0.1:5002/api/aimodelapp/expression-maker', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: text,
style: style
})
});
const endTime = Date.now();
const duration = ((endTime - startTime) / 1000).toFixed(2);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || `HTTP ${response.status}`);
}
const data = await response.json();
if (data.success) {
let resultText = `✅ API调用成功\n耗时: ${duration}\n\n`;
resultText += `表情结果:\n${JSON.stringify(data.expressions, null, 2)}`;
showResult(resultDiv, resultText, 'success');
} else {
throw new Error(data.error || '未知错误');
}
} catch (error) {
const endTime = Date.now();
const duration = ((endTime - startTime) / 1000).toFixed(2);
showResult(resultDiv, `❌ API调用失败\n耗时: ${duration}\n错误: ${error.message}`, 'error');
}
}
async function testVariableAPI() {
const description = document.getElementById('variableDesc').value.trim();
const resultDiv = document.getElementById('variableResult');
if (!description) {
showResult(resultDiv, '请输入变量描述', 'error');
return;
}
showResult(resultDiv, '正在调用变量命名API...\n请耐心等待最多90秒', 'loading');
const startTime = Date.now();
try {
const response = await fetch('http://127.0.0.1:5002/api/aimodelapp/variable-naming', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
description: description
})
});
const endTime = Date.now();
const duration = ((endTime - startTime) / 1000).toFixed(2);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || `HTTP ${response.status}`);
}
const data = await response.json();
if (data.success) {
let resultText = `✅ API调用成功\n耗时: ${duration}\n\n`;
resultText += `变量建议:\n${JSON.stringify(data.suggestions, null, 2)}`;
showResult(resultDiv, resultText, 'success');
} else {
throw new Error(data.error || '未知错误');
}
} catch (error) {
const endTime = Date.now();
const duration = ((endTime - startTime) / 1000).toFixed(2);
showResult(resultDiv, `❌ API调用失败\n耗时: ${duration}\n错误: ${error.message}`, 'error');
}
}
function showResult(element, message, type) {
element.innerHTML = `<div class="result ${type}"><div class="status">${getStatusText(type)}</div>${message}</div>`;
}
function getStatusText(type) {
switch(type) {
case 'success': return '✅ 成功';
case 'error': return '❌ 错误';
case 'loading': return '⏳ 加载中';
default: return '';
}
}
</script>
</body>
</html>