File size: 3,183 Bytes
de2a5d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import subprocess
import threading
import queue
import os
import sys

st.set_page_config(page_title="Claude Code Web UI", page_icon="💻")

st.title("💻 Claude Code Web UI")
st.markdown("Interface pour Claude Code avec Qwen2.5-Coder-7B (GGUF) local")

if "messages" not in st.session_state:
    st.session_state.messages = []

for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

def run_command(command, output_queue):
    try:
        # Ensure claude-code uses the local LLM server
        # This might require setting environment variables or a config file for claude-code
        # For now, we assume claude-code can be configured to use an OpenAI-compatible endpoint
        # or we might need to modify its source/config if it's hardcoded to Anthropic API.
        # Let's try to pass the API base URL as an environment variable if claude-code supports it.
        # If not, this part will need further investigation.
        
        # For demonstration, we'll just run a simple command or simulate claude-code interaction.
        # A real integration would involve piping input/output to the claude-code CLI.
        
        # Example: running a simple shell command for now
        process = subprocess.Popen(
            command, 
            shell=True, 
            stdout=subprocess.PIPE, 
            stderr=subprocess.STDOUT, 
            text=True, 
            bufsize=1
        )
        for line in iter(process.stdout.readline, ""):
            output_queue.put(line)
        process.stdout.close()
        process.wait()
        output_queue.put(None)
    except Exception as e:
        output_queue.put(f"Error executing command: {e}\n")
        output_queue.put(None)

if prompt := st.chat_input("Entrez votre commande Claude Code ou shell..."):
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)

    with st.chat_message("assistant"):
        response_placeholder = st.empty()
        full_response = ""
        output_queue = queue.Queue()
        
        # Prepend 'claude-code' if the user just types a command, or allow direct shell commands
        command_to_run = prompt
        if not prompt.startswith("claude-code") and not prompt.startswith("npm") and not prompt.startswith("python") and not prompt.startswith("ls"):
            command_to_run = f"claude-code {prompt}"

        thread = threading.Thread(target=run_command, args=([command_to_run], output_queue))
        thread.start()
        
        while True:
            try:
                line = output_queue.get(timeout=1)
                if line is None:
                    break
                full_response += line
                response_placeholder.markdown(f"```bash\n{full_response}▌\n```")
            except queue.Empty:
                if not thread.is_alive():
                    break
                continue
        
        response_placeholder.markdown(f"```bash\n{full_response}\n```")
        st.session_state.messages.append({"role": "assistant", "content": full_response})