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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# 使用可能なモデルのリスト
|
| 5 |
+
MODELS = {
|
| 6 |
+
"mistral_saba": "mistral-saba-24b",
|
| 7 |
+
"qwen_coder": "qwen-2.5-coder-32b",
|
| 8 |
+
"deepseek": "deepseek-r1-distill-llama-70b"
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
# 動作モードの定義
|
| 12 |
+
MODES = ["通常モード", "実装モード", "エラー修正モード"]
|
| 13 |
+
|
| 14 |
+
# モード説明の表示
|
| 15 |
+
MODE_DESCRIPTIONS = {
|
| 16 |
+
"通常モード": "Mistral Sabaがユーザー入力を分析し、必要に応じてDeepSeekの知識も活用した上で、Qwen Coderが回答を生成します。",
|
| 17 |
+
"実装モード": "ユーザー入力の分析に基づき、Qwen Coderが設計書を作成した後、実装コードを提供します。",
|
| 18 |
+
"エラー修正モード": "提示されたコードのエラーを分析し、Qwen Coderが修正案を提供します。"
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
# サイドバーの設定UI
|
| 22 |
+
def setup_config(compact=False):
|
| 23 |
+
"""サイドバーの設定UIをセットアップする"""
|
| 24 |
+
st.title("コーディングアシスタント 💻" if not compact else "設定 ⚙️")
|
| 25 |
+
|
| 26 |
+
# モード選択タブ
|
| 27 |
+
selected_mode = st.radio(
|
| 28 |
+
"モード選択",
|
| 29 |
+
MODES,
|
| 30 |
+
key="mode_selection"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# 選択されたモードの説明
|
| 34 |
+
if not compact:
|
| 35 |
+
st.markdown(f"**{selected_mode}**: {MODE_DESCRIPTIONS[selected_mode]}")
|
| 36 |
+
|
| 37 |
+
# モード変更の検出
|
| 38 |
+
if st.session_state.current_mode != selected_mode:
|
| 39 |
+
st.session_state.current_mode = selected_mode
|
| 40 |
+
st.session_state.is_first_message = True
|
| 41 |
+
|
| 42 |
+
# モード変更時にこれまでのチャットを要約
|
| 43 |
+
if not compact and len(st.session_state.messages) > 0:
|
| 44 |
+
with st.spinner("前回の会話内容を要約しています..."):
|
| 45 |
+
from chat import summarize_chat
|
| 46 |
+
try:
|
| 47 |
+
summary = summarize_chat()
|
| 48 |
+
st.info(f"モードが変更されました。前回の会話の要約:\n\n{summary}")
|
| 49 |
+
except Exception as e:
|
| 50 |
+
st.error(f"会話要約の生成中にエラーが発生しました: {str(e)}")
|
| 51 |
+
|
| 52 |
+
# ファイル管理セクション
|
| 53 |
+
if not compact:
|
| 54 |
+
show_file_management()
|
| 55 |
+
else:
|
| 56 |
+
if st.button("ファイル管理", use_container_width=True):
|
| 57 |
+
st.session_state.show_file_dialog = True
|
| 58 |
+
|
| 59 |
+
# ファイル管理ダイアログ(コンパクトモードの場合)
|
| 60 |
+
if compact and st.session_state.get("show_file_dialog", False):
|
| 61 |
+
with st.expander("ファイル管理", expanded=True):
|
| 62 |
+
show_file_management()
|
| 63 |
+
if st.button("閉じる"):
|
| 64 |
+
st.session_state.show_file_dialog = False
|
| 65 |
+
st.rerun()
|
| 66 |
+
|
| 67 |
+
# AI編集モード
|
| 68 |
+
if not compact:
|
| 69 |
+
st.checkbox("AIに編集を任せる", value=st.session_state.ai_edit_mode, key="ai_edit_mode_toggle")
|
| 70 |
+
|
| 71 |
+
if st.session_state.ai_edit_mode:
|
| 72 |
+
st.session_state.edit_instructions = st.text_area(
|
| 73 |
+
"AI編集の指示",
|
| 74 |
+
value=st.session_state.edit_instructions,
|
| 75 |
+
placeholder="例: インデントを修正して、コメントを追加してください",
|
| 76 |
+
height=100
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# チャット履歴のクリア機能
|
| 80 |
+
if st.button("会話履歴をクリア", use_container_width=True):
|
| 81 |
+
st.session_state.messages = []
|
| 82 |
+
st.session_state.thinking_process = []
|
| 83 |
+
st.session_state.is_first_message = True
|
| 84 |
+
st.session_state.conversation_state = {
|
| 85 |
+
"topic": None,
|
| 86 |
+
"last_question_type": None,
|
| 87 |
+
"mentioned_entities": set(),
|
| 88 |
+
"follow_up_count": 0
|
| 89 |
+
}
|
| 90 |
+
st.rerun()
|
| 91 |
+
|
| 92 |
+
# 使い方ガイド(非コンパクトモードのみ)
|
| 93 |
+
if not compact:
|
| 94 |
+
show_help_guides()
|
| 95 |
+
|
| 96 |
+
def show_file_management():
|
| 97 |
+
"""ファイル管理UIを表示する"""
|
| 98 |
+
st.header("ファイル管理")
|
| 99 |
+
|
| 100 |
+
# 新規ファイル作成
|
| 101 |
+
new_file_name = st.text_input("新規ファイル名", value="", key="new_file")
|
| 102 |
+
new_file_col1, new_file_col2 = st.columns([3, 1])
|
| 103 |
+
|
| 104 |
+
with new_file_col1:
|
| 105 |
+
file_extension = st.selectbox(
|
| 106 |
+
"拡張子",
|
| 107 |
+
options=[".py", ".js", ".html", ".css", ".json", ".txt"]
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
with new_file_col2:
|
| 111 |
+
if st.button("作成", key="create_file_btn", use_container_width=True):
|
| 112 |
+
full_filename = new_file_name + file_extension
|
| 113 |
+
if full_filename not in st.session_state.files and new_file_name:
|
| 114 |
+
st.session_state.files[full_filename] = ""
|
| 115 |
+
st.session_state.current_file = full_filename
|
| 116 |
+
st.success(f"ファイル '{full_filename}' を作成しました")
|
| 117 |
+
st.rerun()
|
| 118 |
+
elif not new_file_name:
|
| 119 |
+
st.error("ファイル名を入力してください")
|
| 120 |
+
else:
|
| 121 |
+
st.error(f"ファイル '{full_filename}' は既に存在します")
|
| 122 |
+
|
| 123 |
+
# 既存ファイル一覧
|
| 124 |
+
if st.session_state.files:
|
| 125 |
+
st.subheader("既存ファイル")
|
| 126 |
+
for filename in st.session_state.files.keys():
|
| 127 |
+
col1, col2, col3 = st.columns([3, 1, 1])
|
| 128 |
+
with col1:
|
| 129 |
+
if st.button(filename, key=f"open_{filename}"):
|
| 130 |
+
st.session_state.current_file = filename
|
| 131 |
+
st.rerun()
|
| 132 |
+
with col2:
|
| 133 |
+
if st.button("削除", key=f"delete_{filename}"):
|
| 134 |
+
del st.session_state.files[filename]
|
| 135 |
+
if st.session_state.current_file == filename:
|
| 136 |
+
st.session_state.current_file = None
|
| 137 |
+
st.rerun()
|
| 138 |
+
with col3:
|
| 139 |
+
if st.button("コピー", key=f"copy_{filename}"):
|
| 140 |
+
name, ext = os.path.splitext(filename)
|
| 141 |
+
new_name = f"{name}_copy{ext}"
|
| 142 |
+
counter = 1
|
| 143 |
+
while new_name in st.session_state.files:
|
| 144 |
+
new_name = f"{name}_copy_{counter}{ext}"
|
| 145 |
+
counter += 1
|
| 146 |
+
st.session_state.files[new_name] = st.session_state.files[filename]
|
| 147 |
+
st.rerun()
|
| 148 |
+
|
| 149 |
+
def show_help_guides():
|
| 150 |
+
"""使い方ガイドを表示する"""
|
| 151 |
+
st.markdown("---")
|
| 152 |
+
st.subheader("使い方ガイド")
|
| 153 |
+
|
| 154 |
+
with st.expander("アプリの機能", expanded=False):
|
| 155 |
+
st.markdown("""
|
| 156 |
+
### 主な機能
|
| 157 |
+
1. **コードエディタ** - 複数のプログラミング言語に対応したシンタックスハイライト付きエディタ
|
| 158 |
+
2. **Pythonコード実行** - エディタ上のPythonコードをその場で実行
|
| 159 |
+
3. **AIアシスタント** - コーディングに関する質問や実装の提案
|
| 160 |
+
4. **AIコード編集** - AIにコードの編集を依頼
|
| 161 |
+
5. **ファイル管理** - 複数のファイルの作成・保存・編集
|
| 162 |
+
6. **エラー修正** - コードのエラーを自動で診断・修正
|
| 163 |
+
|
| 164 |
+
### 特殊コマンド
|
| 165 |
+
- `/mode 通常` - 通常の質問応答モードに切り替え
|
| 166 |
+
- `/mode 実装` - コード実装モードに切り替え
|
| 167 |
+
- `/mode エラー修正` - エラー修正モードに切り替え
|
| 168 |
+
- `/edit` - 現在のコードをAIに編集させる
|
| 169 |
+
""")
|
| 170 |
+
|
| 171 |
+
with st.expander("モバイル利用のコツ", expanded=False):
|
| 172 |
+
st.markdown("""
|
| 173 |
+
### モバイルでの使用Tips
|
| 174 |
+
|
| 175 |
+
1. **タブ切り替え** - エディタとチャットを切り替えて使用します
|
| 176 |
+
2. **横向き表示** - 横向きにすると画面が広く使えます
|
| 177 |
+
3. **AIに編集を任せる** - コード入力が難しい場合はAIに編集を依頼できます
|
| 178 |
+
4. **短いコード** - モバイルでは短く分割したコードが扱いやすいです
|
| 179 |
+
5. **コード保存** - 作業中は定期的にコードを保存しましょう
|
| 180 |
+
""")
|
| 181 |
+
|
| 182 |
+
def load_api_key():
|
| 183 |
+
"""APIキーを環境変数またはSecretsから取得する"""
|
| 184 |
+
# 環境変数から取得
|
| 185 |
+
api_key = os.getenv("GROQ_API_KEY", "")
|
| 186 |
+
|
| 187 |
+
# secrets.tomlから取得(環境変数がない場合)
|
| 188 |
+
if not api_key and hasattr(st, 'secrets') and 'GROQ_API_KEY' in st.secrets:
|
| 189 |
+
api_key = st.secrets['GROQ_API_KEY']
|
| 190 |
+
|
| 191 |
+
return api_key
|