在前面的文章中,我們探討了內容生成、檔案分析和 Word 自動化等核心功能,今天我們要深入「智能助手的記憶中樞」:內容狀態管理系統。
這個系統負責追蹤、管理和協調所有類型的內容狀態,包括 AI 生成的內容、專家分析結果、用戶上傳的檔案內容。
系統使用 app_instance
作為全域狀態容器,管理以下核心變數:
# 生成內容狀態
self.app_instance.last_generated_content # 最新生成的內容
self.app_instance.last_generated_time # 生成時間戳
# 分析結果狀態
self.app_instance.last_analysis_result # 最新分析結果
self.app_instance.last_analysis_time # 分析時間戳
# 檔案內容狀態
self.app_instance.uploaded_file_content # 上傳檔案內容
self.app_instance.last_uploaded_file_content # 檔案內容備份
self.app_instance.last_uploaded_filename # 檔案名稱
self.app_instance.last_upload_time # 上傳時間戳
# 工作流程控制
self.app_instance.waiting_for_file_action # 檔案處理等待標記
def check_available_content_tool(self, input_text: str = "") -> str:
"""檢查當前可用的內容狀態"""
print(f"[DEBUG] ===== check_available_content_tool 被調用了! =====")
print(f"[DEBUG] app_instance 型別: {type(self.app_instance)}")
print(f"[DEBUG] app_instance hasattr last_analysis_result: {hasattr(self.app_instance, 'last_analysis_result')}")
if hasattr(self.app_instance, 'last_analysis_result'):
print(f"[DEBUG] last_analysis_result 長度: {len(self.app_instance.last_analysis_result) if self.app_instance.last_analysis_result else 0}")
print(f"[DEBUG] last_analysis_result 前100字: {self.app_instance.last_analysis_result[:100] if self.app_instance.last_analysis_result else 'None'}")
status_info = []
latest_time = 0
latest_type = ""
# 檢查生成內容
print(f"[DEBUG] 檢查生成內容...")
if (hasattr(self.app_instance, 'last_generated_content') and
self.app_instance.last_generated_content and
self.app_instance.last_generated_content.strip()):
gen_time = getattr(self.app_instance, 'last_generated_time', 0)
content_preview = self.app_instance.last_generated_content[:100].replace('\n', ' ')
status_info.append(f"✅ 生成內容: {len(self.app_instance.last_generated_content)}字符 (預覽: {content_preview}...)")
print(f"[DEBUG] 找到生成內容,時間戳: {gen_time}, 長度: {len(self.app_instance.last_generated_content)}")
if gen_time > latest_time:
latest_time = gen_time
latest_type = "生成內容"
else:
print(f"[DEBUG] 沒有生成內容")
# 檢查分析結果
print(f"[DEBUG] 檢查分析結果...")
if (hasattr(self.app_instance, 'last_analysis_result') and
self.app_instance.last_analysis_result and
self.app_instance.last_analysis_result.strip()):
analysis_time = getattr(self.app_instance, 'last_analysis_time', 0)
content_preview = self.app_instance.last_analysis_result[:100].replace('\n', ' ')
status_info.append(f"✅ 分析結果: {len(self.app_instance.last_analysis_result)}字符 (預覽: {content_preview}...)")
print(f"[DEBUG] 找到分析結果,時間戳: {analysis_time}, 長度: {len(self.app_instance.last_analysis_result)}")
if analysis_time > latest_time:
latest_time = analysis_time
latest_type = "分析結果"
else:
print(f"[DEBUG] 沒有分析結果")
# 檢查上傳檔案
print(f"[DEBUG] 檢查上傳檔案...")
if (hasattr(self.app_instance, 'uploaded_file_content') and
self.app_instance.uploaded_file_content and
self.app_instance.uploaded_file_content.strip()):
upload_time = getattr(self.app_instance, 'last_upload_time', 0)
content_preview = self.app_instance.uploaded_file_content[:100].replace('\n', ' ')
filename = getattr(self.app_instance, 'last_uploaded_filename', '未知檔案')
status_info.append(f"✅ 上傳檔案({filename}): {len(self.app_instance.uploaded_file_content)}字符 (預覽: {content_preview}...)")
print(f"[DEBUG] 找到上傳檔案,時間戳: {upload_time}, 長度: {len(self.app_instance.uploaded_file_content)}")
if upload_time > latest_time:
latest_time = upload_time
latest_type = "上傳檔案內容"
else:
print(f"[DEBUG] 沒有上傳檔案")
print(f"[DEBUG] 最終狀態信息數量: {len(status_info)}")
if not status_info:
return "❌ 目前沒有可用的內容。請先生成內容、分析檔案或上傳檔案。"
result = "📋 當前可用內容狀態:\n\n" + "\n".join(status_info)
if latest_type:
result += f"\n\n🔥 最新內容: {latest_type}"
result += f"\n💡 提示: 使用「將已生成內容寫入Word」可以將最新內容寫入Word"
print(f"[DEBUG] 回傳結果: {result[:200]}...")
return result
# 在 generate_content_tool 中的狀態保存
response = self.content_model.generate_content(prompt)
generated_content = response.text
print(f"[DEBUG] Gemini回傳內容長度: {len(generated_content)}")
print(f"[DEBUG] Gemini回傳內容前100字: {generated_content[:100]}...")
# 轉換為繁體中文
generated_content = self.opencc_converter.convert(generated_content)
print(f"[DEBUG] 轉換繁體中文後長度: {len(generated_content)}")
# 保存生成的內容供後續使用
if self.app_instance:
import time
self.app_instance.last_generated_content = generated_content
self.app_instance.last_generated_time = time.time()
print(f"[DEBUG] 已保存生成的內容,長度: {len(generated_content)},時間戳: {self.app_instance.last_generated_time}")
# 在 analyze_file_content_tool 中的狀態保存
response = self.content_model.generate_content(analysis_prompt)
analysis_result = response.text
# 轉換為繁體中文
analysis_result = self.opencc_converter.convert(analysis_result)
# 保存分析結果到app_instance,供後續Word操作使用
if self.app_instance:
import time
self.app_instance.last_analysis_result = analysis_result
self.app_instance.last_analysis_time = time.time()
print(f"[DEBUG] 已保存分析結果到app_instance,長度: {len(analysis_result)},時間戳: {self.app_instance.last_analysis_time}")
# 在 upload_file_tool 中的狀態保存
if result['status'] == 'success':
# 儲存檔案內容供後續使用(統一變數名稱)
if self.app_instance:
import time
self.app_instance.last_uploaded_file_content = result['content']
self.app_instance.uploaded_file_content = result['content'] # 統一變數名稱
self.app_instance.last_uploaded_filename = filename
self.app_instance.last_upload_time = time.time()
print(f"[DEBUG] 已保存上傳檔案內容,長度: {len(result['content'])},時間戳: {self.app_instance.last_upload_time}")
# 設置等待用戶回覆的標記
self.app_instance.waiting_for_file_action = True
# 在 open_word_tool 中的智能內容選擇
# 檢查內容來源優先順序:
# 1. 如果有具體內容,直接使用
# 2. 如果是空內容或要求解答/分析結果,按時間戳檢查最新內容
if (not content.strip() or
content.strip() in ["", "解答", "分析結果", "答案"] or
"解答" in content and len(content.strip()) < 10):
print(f"[DEBUG] 檢測到空內容或請求解答,檢查所有可用內容...")
latest_time = 0
latest_content = ""
latest_source = ""
# 檢查分析結果
if (hasattr(self.app_instance, 'last_analysis_result') and
self.app_instance.last_analysis_result.strip()):
analysis_time = getattr(self.app_instance, 'last_analysis_time', 0)
if analysis_time > latest_time:
latest_time = analysis_time
latest_content = self.app_instance.last_analysis_result
latest_source = "分析結果"
# 檢查生成內容
if (hasattr(self.app_instance, 'last_generated_content') and
self.app_instance.last_generated_content.strip()):
gen_time = getattr(self.app_instance, 'last_generated_time', 0)
if gen_time > latest_time:
latest_time = gen_time
latest_content = self.app_instance.last_generated_content
latest_source = "生成內容"
# 檢查上傳檔案
if (hasattr(self.app_instance, 'uploaded_file_content') and
self.app_instance.uploaded_file_content.strip()):
upload_time = getattr(self.app_instance, 'last_upload_time', 0)
if upload_time > latest_time:
latest_time = upload_time
latest_content = self.app_instance.uploaded_file_content
latest_source = "上傳檔案內容"
if latest_content:
content = latest_content
print(f"[DEBUG] open_word_tool: 使用最新的{latest_source},時間戳: {latest_time},長度: {len(content)}")
else:
print(f"[DEBUG] open_word_tool: 沒有找到任何保存的內容,使用預設內容")
content = "您沒有指定內容,這是預設文字。"
內容狀態管理系統是智能助手的「記憶與協調中樞」,它:
這個系統讓 AI 助手不再是無記憶的對話機器人,而是具備完整記憶和狀態管理能力的智能工作夥伴,能夠在複雜的多步驟工作流程中保持內容的連貫性和一致性。