mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-05-19 06:07:28 +08:00
mobile ux + logs: hide title on phone and preload last 10 logs
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
@@ -78,6 +79,7 @@ func (s *RegistryServer) Start(ctx context.Context) error {
|
|||||||
mux.HandleFunc("/webui/api/memory", s.handleWebUIMemory)
|
mux.HandleFunc("/webui/api/memory", s.handleWebUIMemory)
|
||||||
mux.HandleFunc("/webui/api/exec_approvals", s.handleWebUIExecApprovals)
|
mux.HandleFunc("/webui/api/exec_approvals", s.handleWebUIExecApprovals)
|
||||||
mux.HandleFunc("/webui/api/logs/stream", s.handleWebUILogsStream)
|
mux.HandleFunc("/webui/api/logs/stream", s.handleWebUILogsStream)
|
||||||
|
mux.HandleFunc("/webui/api/logs/recent", s.handleWebUILogsRecent)
|
||||||
s.server = &http.Server{Addr: s.addr, Handler: mux}
|
s.server = &http.Server{Addr: s.addr, Handler: mux}
|
||||||
go func() {
|
go func() {
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
@@ -986,6 +988,57 @@ func (s *RegistryServer) handleWebUIExecApprovals(w http.ResponseWriter, r *http
|
|||||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *RegistryServer) handleWebUILogsRecent(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !s.checkAuth(r) {
|
||||||
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
path := strings.TrimSpace(s.logFilePath)
|
||||||
|
if path == "" {
|
||||||
|
http.Error(w, "log path not configured", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
limit := 10
|
||||||
|
if v := strings.TrimSpace(r.URL.Query().Get("limit")); v != "" {
|
||||||
|
if n, err := strconv.Atoi(v); err == nil && n > 0 && n <= 200 {
|
||||||
|
limit = n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
lines := strings.Split(strings.ReplaceAll(string(b), "\r\n", "\n"), "\n")
|
||||||
|
if len(lines) > 0 && strings.TrimSpace(lines[len(lines)-1]) == "" {
|
||||||
|
lines = lines[:len(lines)-1]
|
||||||
|
}
|
||||||
|
start := 0
|
||||||
|
if len(lines) > limit {
|
||||||
|
start = len(lines) - limit
|
||||||
|
}
|
||||||
|
out := make([]map[string]interface{}, 0, limit)
|
||||||
|
for _, ln := range lines[start:] {
|
||||||
|
ln = strings.TrimSpace(ln)
|
||||||
|
if ln == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if json.Valid([]byte(ln)) {
|
||||||
|
var m map[string]interface{}
|
||||||
|
if err := json.Unmarshal([]byte(ln), &m); err == nil {
|
||||||
|
out = append(out, m)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out = append(out, map[string]interface{}{"time": time.Now().UTC().Format(time.RFC3339), "level": "INFO", "msg": ln})
|
||||||
|
}
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{"ok": true, "logs": out})
|
||||||
|
}
|
||||||
|
|
||||||
func (s *RegistryServer) handleWebUILogsStream(w http.ResponseWriter, r *http.Request) {
|
func (s *RegistryServer) handleWebUILogsStream(w http.ResponseWriter, r *http.Request) {
|
||||||
if !s.checkAuth(r) {
|
if !s.checkAuth(r) {
|
||||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ const Header: React.FC = () => {
|
|||||||
<div className="w-8 h-8 md:w-9 md:h-9 rounded-xl bg-indigo-500 flex items-center justify-center shadow-lg shadow-indigo-500/20 shrink-0">
|
<div className="w-8 h-8 md:w-9 md:h-9 rounded-xl bg-indigo-500 flex items-center justify-center shadow-lg shadow-indigo-500/20 shrink-0">
|
||||||
<Terminal className="w-4 h-4 md:w-5 md:h-5 text-white" />
|
<Terminal className="w-4 h-4 md:w-5 md:h-5 text-white" />
|
||||||
</div>
|
</div>
|
||||||
<span className="font-semibold text-lg md:text-xl tracking-tight truncate">ClawGo</span>
|
<span className="hidden md:inline font-semibold text-lg md:text-xl tracking-tight truncate">ClawGo</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex items-center gap-2 md:gap-6">
|
<div className="flex items-center gap-2 md:gap-6">
|
||||||
|
|||||||
@@ -12,6 +12,19 @@ const Logs: React.FC = () => {
|
|||||||
const logEndRef = useRef<HTMLDivElement>(null);
|
const logEndRef = useRef<HTMLDivElement>(null);
|
||||||
const abortControllerRef = useRef<AbortController | null>(null);
|
const abortControllerRef = useRef<AbortController | null>(null);
|
||||||
|
|
||||||
|
const loadRecent = async () => {
|
||||||
|
try {
|
||||||
|
const r = await fetch(`/webui/api/logs/recent${q ? `${q}&limit=10` : '?limit=10'}`);
|
||||||
|
if (!r.ok) return;
|
||||||
|
const j = await r.json();
|
||||||
|
if (Array.isArray(j.logs)) {
|
||||||
|
setLogs(j.logs as LogEntry[]);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('load recent logs failed', e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const startStreaming = async () => {
|
const startStreaming = async () => {
|
||||||
if (abortControllerRef.current) abortControllerRef.current.abort();
|
if (abortControllerRef.current) abortControllerRef.current.abort();
|
||||||
abortControllerRef.current = new AbortController();
|
abortControllerRef.current = new AbortController();
|
||||||
@@ -51,6 +64,7 @@ const Logs: React.FC = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
loadRecent();
|
||||||
if (isStreaming) {
|
if (isStreaming) {
|
||||||
startStreaming();
|
startStreaming();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user