Files
hr-position-system/scripts/quick_fix.py
DonaldFang 方士碩 a6af297623 backup: 完成 HR_position_ 表格前綴重命名與欄位對照表整理
變更內容:
- 所有資料表加上 HR_position_ 前綴
- 整理完整欄位顯示名稱與 ID 對照表
- 模組化 JS 檔案 (admin.js, ai.js, csv.js 等)
- 專案結構優化 (docs/, scripts/, tests/ 等)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-09 12:05:20 +08:00

111 lines
3.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import re
with open('index.html', 'r', encoding='utf-8') as f:
content = f.read()
# 備份
with open('index.html.backup', 'w', encoding='utf-8') as f:
f.write(content)
print("Backup created: index.html.backup")
# 舊代碼
old = ''' async function callClaudeAPI(prompt) {
try {
const response = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "claude-sonnet-4-20250514",
max_tokens: 2000,
messages: [
{ role: "user", content: prompt }
]
})
});
if (!response.ok) {
throw new Error(`API request failed: ${response.status}`);
}
const data = await response.json();
let responseText = data.content[0].text;
responseText = responseText.replace(/```json\\n?/g, "").replace(/```\\n?/g, "").trim();
return JSON.parse(responseText);
} catch (error) {
console.error("Error calling Claude API:", error);
throw error;
}
}'''
# 新代碼
new = ''' async function callClaudeAPI(prompt, api = 'gemini') {
try {
// 調用後端 Flask API避免 CORS 錯誤
const response = await fetch("/api/llm/generate", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
api: api,
prompt: prompt,
max_tokens: 2000
})
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || `API 請求失敗: ${response.status}`);
}
const data = await response.json();
if (!data.success) {
throw new Error(data.error || 'API 調用失敗');
}
let responseText = data.text;
responseText = responseText.replace(/```json\\n?/g, "").replace(/```\\n?/g, "").trim();
return JSON.parse(responseText);
} catch (error) {
console.error("Error calling LLM API:", error);
alert(`AI 生成錯誤: ${error.message}\\n\\n請確保\\n1. Flask 後端已啟動 (python app_updated.py)\\n2. 已在 .env 文件中配置 LLM API Key\\n3. 網路連線正常`);
throw error;
}
}'''
# 替換
new_content = content.replace(old, new)
if new_content == content:
print("ERROR: Pattern not found, trying alternative method...")
# 使用更簡單的替換
new_content = content.replace(
'const response = await fetch("https://api.anthropic.com/v1/messages", {',
'const response = await fetch("/api/llm/generate", {'
)
new_content = new_content.replace(
'async function callClaudeAPI(prompt) {',
'async function callClaudeAPI(prompt, api = \'gemini\') {'
)
if new_content != content:
print("SUCCESS: Applied simple replacement")
else:
print("ERROR: Could not fix the file")
exit(1)
else:
print("SUCCESS: Pattern replaced")
# 寫回
with open('index.html', 'w', encoding='utf-8') as f:
f.write(new_content)
print("File updated: index.html")
print("\nNext steps:")
print("1. Start Flask backend: python app_updated.py")
print("2. Reload browser page (Ctrl+F5)")
print("3. Test AI generation")