File size: 4,179 Bytes
ca44502
8a6a561
 
 
 
ca44502
7a21f64
 
 
ca44502
8a6a561
ca44502
8a6a561
ca44502
8a6a561
ca44502
 
8a6a561
 
 
 
dde7f08
 
 
 
 
ca44502
8a6a561
 
a5e26cc
8a6a561
 
a5e26cc
8a6a561
 
a5e26cc
8a6a561
 
ca44502
8a6a561
 
 
ca44502
8a6a561
 
65ed2e4
8a6a561
 
6003912
8a6a561
 
6003912
8a6a561
 
6003912
8a6a561
 
 
 
 
 
 
 
 
 
 
 
ca44502
dde7f08
ca44502
 
 
 
8a6a561
ca44502
8a6a561
 
 
 
 
 
 
 
 
 
 
1bf3490
 
 
 
 
 
 
 
 
8a6a561
1bf3490
 
 
 
 
 
 
8a6a561
 
 
1bf3490
8a6a561
1bf3490
8a6a561
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import streamlit as st
from editor import setup_editor
from chat import setup_chat
from config import setup_config, load_api_key
from utils import create_default_prompt_files
import os

# デフォルトのプロンプトファイルを作成
create_default_prompt_files()

# アプリ設定
st.set_page_config(
    page_title="コーディングアシスタント",
    page_icon="💻",
    layout="centered"  # モバイルフレンドリーな中央寄せレイアウト
)

# APIキーの取得
api_key = load_api_key()

# セッションの初期化
if "files" not in st.session_state:
    st.session_state.files = {}  # ファイル名: コンテンツ

if "current_file" not in st.session_state:
    st.session_state.current_file = None

if "messages" not in st.session_state:
    st.session_state.messages = []
    
if "thinking_process" not in st.session_state:
    st.session_state.thinking_process = []
    
if "current_mode" not in st.session_state:
    st.session_state.current_mode = "通常モード"
    
if "is_first_message" not in st.session_state:
    st.session_state.is_first_message = True

if "mobile_mode" not in st.session_state:
    # デバイスの画面幅に基づいて自動判定(JavaScript使用)
    st.session_state.mobile_mode = True

if "active_tab" not in st.session_state:
    st.session_state.active_tab = "エディタ"

if "last_generated_code" not in st.session_state:
    st.session_state.last_generated_code = None
    
if "ai_edit_mode" not in st.session_state:
    st.session_state.ai_edit_mode = False
    
if "edit_instructions" not in st.session_state:
    st.session_state.edit_instructions = ""

# 会話状態の初期化
if "conversation_state" not in st.session_state:
    st.session_state.conversation_state = {
        "topic": None,
        "last_question_type": None,
        "mentioned_entities": set(),
        "follow_up_count": 0
    }

# モバイルモードトグル
mobile_mode = st.checkbox("モバイルモード", value=st.session_state.mobile_mode, key="mobile_toggle")
st.session_state.mobile_mode = mobile_mode

# APIキーがない場合の警告表示
if not api_key:
    st.warning("""
    ### ⚠️ GROQ_API_KEYが設定されていません
    
    このアプリを使用するには、GROQ_API_KEYを設定する必要があります:
    
    1. Hugging Face Spacesの場合: Spacesのダッシュボード -> Settings -> Repository secrets
    2. ローカル環境の場合: .streamlit/secrets.toml または環境変数
    """)
    st.stop()

# モバイルモードの場合はタブで切り替え、デスクトップモードの場合は並べて表示
if st.session_state.mobile_mode:
    # サイドバーには最小限の設定のみを表示
    with st.sidebar:
        setup_config(compact=True)
    
    # モード選択用のラジオボタン(横並び)
    col1, col2 = st.columns(2)
    with col1:
        editor_selected = st.button("📝 エディタ", 
                                  use_container_width=True,
                                  type="primary" if st.session_state.active_tab == "エディタ" else "secondary")
        if editor_selected:
            st.session_state.active_tab = "エディタ"
            st.rerun()
    
    with col2:
        chat_selected = st.button("💬 チャット", 
                                use_container_width=True,
                                type="primary" if st.session_state.active_tab == "チャット" else "secondary")
        if chat_selected:
            st.session_state.active_tab = "チャット"
            st.rerun()
    
    # アクティブなタブに応じてコンテンツを表示
    if st.session_state.active_tab == "エディタ":
        setup_editor(api_key=api_key, mobile=True)
    else:
        setup_chat(api_key=api_key, mobile=True)
else:
    # サイドバー設定
    with st.sidebar:
        setup_config(compact=False)
    
    # コードエディタとチャットを並べて表示
    col1, col2 = st.columns([1, 1])
    
    with col1:
        setup_editor(api_key=api_key, mobile=False)
    
    with col2:
        setup_chat(api_key=api_key, mobile=False)