mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-05-09 09:27:29 +08:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ccf2ed4703 | ||
|
|
09c06786a4 | ||
|
|
57961d2911 | ||
|
|
764fa94ced | ||
|
|
b007a403a1 |
@@ -1,13 +0,0 @@
|
||||
FROM golang:alpine AS builder
|
||||
WORKDIR /app
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
COPY . .
|
||||
# Sync root workspace templates into embed path before build
|
||||
RUN rm -rf ./cmd/clawgo/workspace && mkdir -p ./cmd/clawgo/workspace && cp -a ./workspace/. ./cmd/clawgo/workspace/
|
||||
RUN go build -o clawgo ./cmd/clawgo
|
||||
|
||||
FROM alpine:latest
|
||||
WORKDIR /root/
|
||||
COPY --from=builder /app/clawgo .
|
||||
CMD ["./clawgo"]
|
||||
17
README.md
17
README.md
@@ -19,13 +19,28 @@
|
||||
|
||||
## 并发调度(新)⚙️
|
||||
|
||||
- 🧩 一条复合消息会先自动拆成多个子任务(可配置上限)
|
||||
- 🧩 一条复合消息会先自动拆成多个子任务
|
||||
- 🔀 同一会话内:无资源冲突的子任务可并发执行
|
||||
- 🔒 同一会话内:有资源冲突的子任务自动串行(避免互相踩)
|
||||
- 🏷️ 默认会自动推断 `resource_keys`,无需手动填写
|
||||
|
||||
---
|
||||
|
||||
## 记忆与 EKG 增强(新)🧠
|
||||
|
||||
- 📚 每个子任务执行前会自动检索相关记忆(`memory_search`)
|
||||
- 🚨 会结合 EKG 与任务审计识别“重复错误签名”风险
|
||||
- ⏱️ 高风险任务会自动附带重试退避建议,减少重复踩坑
|
||||
|
||||
---
|
||||
|
||||
## Telegram 流式渲染优化(新)💬
|
||||
|
||||
- 🧷 只在语法安全断点刷新流式内容(减少格式抖动)
|
||||
- ✅ 结束时做一次最终收敛渲染,保证排版稳定
|
||||
|
||||
---
|
||||
|
||||
## 3 分钟上手 ⚡
|
||||
|
||||
### 1) 安装
|
||||
|
||||
17
README_EN.md
17
README_EN.md
@@ -19,13 +19,28 @@ A long-running AI Agent written in Go: lightweight, auditable, and multi-channel
|
||||
|
||||
## Concurrency Scheduling (New) ⚙️
|
||||
|
||||
- 🧩 A composite message is auto-split into sub-tasks (configurable max)
|
||||
- 🧩 A composite message is automatically split into sub-tasks
|
||||
- 🔀 Within the same session: non-conflicting tasks run in parallel
|
||||
- 🔒 Within the same session: conflicting tasks run serially (to avoid collisions)
|
||||
- 🏷️ `resource_keys` are inferred automatically by default (no manual input needed)
|
||||
|
||||
---
|
||||
|
||||
## Memory + EKG Enhancements (New) 🧠
|
||||
|
||||
- 📚 Before each sub-task runs, related memory is fetched via `memory_search`
|
||||
- 🚨 EKG + task-audit are used to detect repeated error-signature risks
|
||||
- ⏱️ High-risk tasks include retry-backoff hints to reduce repeated failures
|
||||
|
||||
---
|
||||
|
||||
## Telegram Streaming Rendering (New) 💬
|
||||
|
||||
- 🧷 Stream flushes happen at syntax-safe boundaries to reduce formatting jitter
|
||||
- ✅ A final convergence render is applied when streaming ends for stable layout
|
||||
|
||||
---
|
||||
|
||||
## 3-Minute Quick Start ⚡
|
||||
|
||||
### 1) Install
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"clawgo/pkg/bus"
|
||||
"clawgo/pkg/cron"
|
||||
)
|
||||
|
||||
func TestNormalizeCronTargetChatID(t *testing.T) {
|
||||
if got := normalizeCronTargetChatID("telegram", "telegram:12345"); got != "12345" {
|
||||
t.Fatalf("expected 12345, got %q", got)
|
||||
}
|
||||
if got := normalizeCronTargetChatID("telegram", "12345"); got != "12345" {
|
||||
t.Fatalf("expected unchanged chat id, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispatchCronJob_DeliversTargetedMessageEvenWhenDeliverFalse(t *testing.T) {
|
||||
mb := bus.NewMessageBus()
|
||||
defer mb.Close()
|
||||
|
||||
status := dispatchCronJob(mb, &cron.CronJob{
|
||||
ID: "job-1",
|
||||
Payload: cron.CronPayload{
|
||||
Message: "time to sleep",
|
||||
Deliver: false,
|
||||
Channel: "telegram",
|
||||
To: "telegram:5988738763",
|
||||
},
|
||||
})
|
||||
|
||||
if status != "delivered_targeted" {
|
||||
t.Fatalf("unexpected status: %s", status)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
out, ok := mb.SubscribeOutbound(ctx)
|
||||
if !ok {
|
||||
t.Fatal("expected outbound message")
|
||||
}
|
||||
if out.Channel != "telegram" || out.ChatID != "5988738763" || out.Content != "time to sleep" {
|
||||
t.Fatalf("unexpected outbound: %#v", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispatchCronJob_FallsBackToSystemInboundWithoutTarget(t *testing.T) {
|
||||
mb := bus.NewMessageBus()
|
||||
defer mb.Close()
|
||||
|
||||
status := dispatchCronJob(mb, &cron.CronJob{ID: "job-2", Payload: cron.CronPayload{Message: "tick"}})
|
||||
if status != "scheduled" {
|
||||
t.Fatalf("unexpected status: %s", status)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
in, ok := mb.ConsumeInbound(ctx)
|
||||
if !ok {
|
||||
t.Fatal("expected inbound message")
|
||||
}
|
||||
if in.Channel != "system" || in.ChatID != "internal:cron" {
|
||||
t.Fatalf("unexpected inbound: %#v", in)
|
||||
}
|
||||
}
|
||||
96
install.sh
96
install.sh
@@ -9,6 +9,35 @@ REPO="clawgo"
|
||||
BIN="clawgo"
|
||||
INSTALL_DIR="/usr/local/bin"
|
||||
WEBUI_DIR="$HOME/.clawgo/workspace/webui"
|
||||
UI_ONLY=0
|
||||
|
||||
usage() {
|
||||
cat <<EOF
|
||||
Usage: $0 [-ui]
|
||||
|
||||
Options:
|
||||
-ui Update WebUI only. Skip binary download/install and migration prompt.
|
||||
-h Show this help message.
|
||||
EOF
|
||||
}
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
-ui)
|
||||
UI_ONLY=1
|
||||
shift
|
||||
;;
|
||||
-h|--help)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
echo "Unknown argument: $1"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# ====================
|
||||
# Detect OS/ARCH
|
||||
@@ -30,9 +59,13 @@ echo "Detected OS=$OS ARCH=$ARCH"
|
||||
# ====================
|
||||
# Check if already installed
|
||||
# ====================
|
||||
if command -v "$BIN" &> /dev/null; then
|
||||
echo "$BIN is already installed. Removing existing version..."
|
||||
sudo rm -f "$INSTALL_DIR/$BIN"
|
||||
if [[ "$UI_ONLY" -eq 0 ]]; then
|
||||
if command -v "$BIN" &> /dev/null; then
|
||||
echo "$BIN is already installed. Removing existing version..."
|
||||
sudo rm -f "$INSTALL_DIR/$BIN"
|
||||
fi
|
||||
else
|
||||
echo "UI-only mode enabled: skip binary uninstall/install."
|
||||
fi
|
||||
|
||||
# ====================
|
||||
@@ -57,36 +90,36 @@ WEBUI_FILE="webui.tar.gz"
|
||||
URL="https://github.com/$OWNER/$REPO/releases/download/$TAG/$FILE"
|
||||
WEBUI_URL="https://github.com/$OWNER/$REPO/releases/download/$TAG/$WEBUI_FILE"
|
||||
|
||||
echo "Trying to download: $URL"
|
||||
|
||||
# Try to download binary release
|
||||
TMPDIR="$(mktemp -d)"
|
||||
OUT="$TMPDIR/$FILE"
|
||||
if [[ "$UI_ONLY" -eq 0 ]]; then
|
||||
echo "Trying to download: $URL"
|
||||
OUT="$TMPDIR/$FILE"
|
||||
|
||||
# Now try downloading the file
|
||||
if curl -fSL "$URL" -o "$OUT"; then
|
||||
echo "Downloaded $FILE"
|
||||
tar -xzf "$OUT" -C "$TMPDIR"
|
||||
# Now try downloading the file
|
||||
if curl -fSL "$URL" -o "$OUT"; then
|
||||
echo "Downloaded $FILE"
|
||||
tar -xzf "$OUT" -C "$TMPDIR"
|
||||
|
||||
EXTRACTED_BIN=""
|
||||
if [[ -f "$TMPDIR/$BIN" ]]; then
|
||||
EXTRACTED_BIN="$TMPDIR/$BIN"
|
||||
EXTRACTED_BIN=""
|
||||
if [[ -f "$TMPDIR/$BIN" ]]; then
|
||||
EXTRACTED_BIN="$TMPDIR/$BIN"
|
||||
else
|
||||
EXTRACTED_BIN="$(find "$TMPDIR" -maxdepth 2 -type f -name "${BIN}*" ! -name "*.tar.gz" ! -name "*.zip" | head -n1)"
|
||||
fi
|
||||
|
||||
if [[ -z "$EXTRACTED_BIN" || ! -f "$EXTRACTED_BIN" ]]; then
|
||||
echo "Failed to locate extracted binary from $FILE"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
chmod +x "$EXTRACTED_BIN"
|
||||
echo "Installing $BIN to $INSTALL_DIR (may require sudo)..."
|
||||
sudo mv "$EXTRACTED_BIN" "$INSTALL_DIR/$BIN"
|
||||
echo "Installed $BIN to $INSTALL_DIR/clawgo"
|
||||
else
|
||||
EXTRACTED_BIN="$(find "$TMPDIR" -maxdepth 2 -type f -name "${BIN}*" ! -name "*.tar.gz" ! -name "*.zip" | head -n1)"
|
||||
fi
|
||||
|
||||
if [[ -z "$EXTRACTED_BIN" || ! -f "$EXTRACTED_BIN" ]]; then
|
||||
echo "Failed to locate extracted binary from $FILE"
|
||||
echo "No prebuilt binary found, exiting..."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
chmod +x "$EXTRACTED_BIN"
|
||||
echo "Installing $BIN to $INSTALL_DIR (may require sudo)..."
|
||||
sudo mv "$EXTRACTED_BIN" "$INSTALL_DIR/$BIN"
|
||||
echo "Installed $BIN to $INSTALL_DIR/clawgo"
|
||||
else
|
||||
echo "No prebuilt binary found, exiting..."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# ====================
|
||||
@@ -123,6 +156,7 @@ fi
|
||||
# ====================
|
||||
# Migrate (Embedded openclaw2clawgo Script)
|
||||
# ====================
|
||||
if [[ "$UI_ONLY" -eq 0 ]]; then
|
||||
read -p "Do you want to migrate your OpenClaw workspace to ClawGo? (y/n): " MIGRATE
|
||||
if [[ "$MIGRATE" == "y" || "$MIGRATE" == "Y" ]]; then
|
||||
echo "Choose migration type: "
|
||||
@@ -239,6 +273,7 @@ done
|
||||
if [[ -d "$DST/memory" ]]; then
|
||||
cp -a "$DST/memory" "$BACKUP_DIR/memory" || true
|
||||
fi
|
||||
fi
|
||||
|
||||
# Migrate core persona/context files
|
||||
for f in AGENTS.md SOUL.md USER.md IDENTITY.md TOOLS.md MEMORY.md HEARTBEAT.md; do
|
||||
@@ -268,9 +303,14 @@ EOF
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "Cleaning up..."
|
||||
rm -rf "$TMPDIR"
|
||||
|
||||
echo "Done 🎉"
|
||||
echo "Run 'clawgo --help' to verify"
|
||||
if [[ "$UI_ONLY" -eq 0 ]]; then
|
||||
echo "Run 'clawgo --help' to verify"
|
||||
else
|
||||
echo "WebUI update finished."
|
||||
fi
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"clawgo/pkg/bus"
|
||||
"clawgo/pkg/config"
|
||||
@@ -452,7 +453,12 @@ func (al *AgentLoop) processInbound(ctx context.Context, msg bus.InboundMessage)
|
||||
}
|
||||
}
|
||||
if msg.Channel == "telegram" && suppressed {
|
||||
al.bus.PublishOutbound(bus.OutboundMessage{Channel: msg.Channel, ChatID: msg.ChatID, Action: "finalize"})
|
||||
replyID := ""
|
||||
if msg.Metadata != nil {
|
||||
replyID = msg.Metadata["message_id"]
|
||||
}
|
||||
// Final pass uses full formatted content to stabilize rendering after plain streaming.
|
||||
al.bus.PublishOutbound(bus.OutboundMessage{Channel: msg.Channel, ChatID: msg.ChatID, Action: "finalize", Content: response, ReplyToID: replyID})
|
||||
}
|
||||
al.audit.Record(trigger, msg.Channel, msg.SessionKey, suppressed, err)
|
||||
al.appendTaskAudit(taskID, msg, started, err, suppressed)
|
||||
@@ -840,11 +846,15 @@ func (al *AgentLoop) processMessage(ctx context.Context, msg bus.InboundMessage)
|
||||
if time.Since(lastPush) < 450*time.Millisecond {
|
||||
return
|
||||
}
|
||||
if !shouldFlushTelegramStreamSnapshot(streamText) {
|
||||
return
|
||||
}
|
||||
lastPush = time.Now()
|
||||
replyID := ""
|
||||
if msg.Metadata != nil {
|
||||
replyID = msg.Metadata["message_id"]
|
||||
}
|
||||
// Stream with formatted rendering once snapshot is syntactically safe.
|
||||
al.bus.PublishOutbound(bus.OutboundMessage{Channel: msg.Channel, ChatID: msg.ChatID, Content: streamText, Action: "stream", ReplyToID: replyID})
|
||||
al.markSessionStreamed(msg.SessionKey)
|
||||
})
|
||||
@@ -1679,6 +1689,32 @@ func shouldDropNoReply(text string) bool {
|
||||
return strings.EqualFold(t, "NO_REPLY")
|
||||
}
|
||||
|
||||
func shouldFlushTelegramStreamSnapshot(s string) bool {
|
||||
s = strings.TrimRight(s, " \t")
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
last, _ := utf8.DecodeLastRuneInString(s)
|
||||
switch last {
|
||||
case '\n', '。', '!', '?', '.', '!', '?', ';', ';', ':', ':':
|
||||
default:
|
||||
return false
|
||||
}
|
||||
// Avoid flushing while code fences are still unbalanced.
|
||||
if strings.Count(s, "```")%2 == 1 {
|
||||
return false
|
||||
}
|
||||
// Avoid flushing while common inline markdown markers are unbalanced.
|
||||
if strings.Count(s, "**")%2 == 1 || strings.Count(s, "__")%2 == 1 || strings.Count(s, "~~")%2 == 1 {
|
||||
return false
|
||||
}
|
||||
// Rough guard for links/images: require bracket balance before flushing.
|
||||
if strings.Count(s, "[") != strings.Count(s, "]") || strings.Count(s, "(") != strings.Count(s, ")") {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func parseReplyTag(text string, currentMessageID string) (content string, replyToID string) {
|
||||
t := strings.TrimSpace(text)
|
||||
if !strings.HasPrefix(t, "[[") {
|
||||
@@ -1724,7 +1760,8 @@ func rewriteSystemMessageContent(content, template string) string {
|
||||
func alSessionListForTool(sm *session.SessionManager, limit int) []tools.SessionInfo {
|
||||
items := sm.List(limit)
|
||||
out := make([]tools.SessionInfo, 0, len(items))
|
||||
for _, s := range items {
|
||||
for i := range items {
|
||||
s := &items[i]
|
||||
out = append(out, tools.SessionInfo{
|
||||
Key: s.Key,
|
||||
Kind: s.Kind,
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"clawgo/pkg/bus"
|
||||
"clawgo/pkg/ekg"
|
||||
"clawgo/pkg/scheduling"
|
||||
)
|
||||
|
||||
@@ -121,7 +126,7 @@ func (al *AgentLoop) runPlannedTasks(ctx context.Context, msg bus.InboundMessage
|
||||
go func(index int, t plannedTask) {
|
||||
defer wg.Done()
|
||||
subMsg := msg
|
||||
subMsg.Content = t.Content
|
||||
subMsg.Content = al.enrichTaskContentWithMemoryAndEKG(ctx, t)
|
||||
subMsg.Metadata = cloneMetadata(msg.Metadata)
|
||||
if subMsg.Metadata == nil {
|
||||
subMsg.Metadata = map[string]string{}
|
||||
@@ -157,6 +162,165 @@ func (al *AgentLoop) runPlannedTasks(ctx context.Context, msg bus.InboundMessage
|
||||
return strings.TrimSpace(b.String()), nil
|
||||
}
|
||||
|
||||
func (al *AgentLoop) enrichTaskContentWithMemoryAndEKG(ctx context.Context, task plannedTask) string {
|
||||
base := strings.TrimSpace(task.Content)
|
||||
if base == "" {
|
||||
return base
|
||||
}
|
||||
hints := make([]string, 0, 2)
|
||||
if mem := al.memoryHintForTask(ctx, task); mem != "" {
|
||||
hints = append(hints, "Memory:\n"+mem)
|
||||
}
|
||||
if risk := al.ekgHintForTask(task); risk != "" {
|
||||
hints = append(hints, "EKG:\n"+risk)
|
||||
}
|
||||
if len(hints) == 0 {
|
||||
return base
|
||||
}
|
||||
return strings.TrimSpace(
|
||||
"Task Context (use it as constraints, avoid repeating known failures):\n" +
|
||||
strings.Join(hints, "\n\n") +
|
||||
"\n\nTask:\n" + base,
|
||||
)
|
||||
}
|
||||
|
||||
func (al *AgentLoop) memoryHintForTask(ctx context.Context, task plannedTask) string {
|
||||
if al == nil || al.tools == nil {
|
||||
return ""
|
||||
}
|
||||
res, err := al.tools.Execute(ctx, "memory_search", map[string]interface{}{
|
||||
"query": task.Content,
|
||||
"maxResults": 2,
|
||||
})
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
txt := strings.TrimSpace(res)
|
||||
if txt == "" || strings.HasPrefix(strings.ToLower(txt), "no memory found") {
|
||||
return ""
|
||||
}
|
||||
return truncate(txt, 1200)
|
||||
}
|
||||
|
||||
func (al *AgentLoop) ekgHintForTask(task plannedTask) string {
|
||||
if al == nil || al.ekg == nil || strings.TrimSpace(al.workspace) == "" {
|
||||
return ""
|
||||
}
|
||||
evt, ok := al.findRecentRelatedErrorEvent(task.Content)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
errSig := ekg.NormalizeErrorSignature(evt.Log)
|
||||
if errSig == "" {
|
||||
return ""
|
||||
}
|
||||
advice := al.ekg.GetAdvice(ekg.SignalContext{
|
||||
TaskID: evt.TaskID,
|
||||
ErrSig: errSig,
|
||||
Source: evt.Source,
|
||||
Channel: evt.Channel,
|
||||
})
|
||||
if !advice.ShouldEscalate {
|
||||
return ""
|
||||
}
|
||||
reasons := strings.Join(advice.Reason, ", ")
|
||||
if strings.TrimSpace(reasons) == "" {
|
||||
reasons = "repeated error signature"
|
||||
}
|
||||
return fmt.Sprintf("Related repeated error signature detected (%s). Suggested retry backoff: %ds. Last error: %s",
|
||||
errSig, advice.RetryBackoffSec, truncate(strings.TrimSpace(evt.Log), 240))
|
||||
}
|
||||
|
||||
type taskAuditErrorEvent struct {
|
||||
TaskID string
|
||||
Source string
|
||||
Channel string
|
||||
Log string
|
||||
Preview string
|
||||
}
|
||||
|
||||
func (al *AgentLoop) findRecentRelatedErrorEvent(taskContent string) (taskAuditErrorEvent, bool) {
|
||||
path := filepath.Join(strings.TrimSpace(al.workspace), "memory", "task-audit.jsonl")
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return taskAuditErrorEvent{}, false
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
kw := tokenizeTaskText(taskContent)
|
||||
if len(kw) == 0 {
|
||||
return taskAuditErrorEvent{}, false
|
||||
}
|
||||
var best taskAuditErrorEvent
|
||||
bestScore := 0
|
||||
|
||||
s := bufio.NewScanner(f)
|
||||
for s.Scan() {
|
||||
line := strings.TrimSpace(s.Text())
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
var row map[string]interface{}
|
||||
if json.Unmarshal([]byte(line), &row) != nil {
|
||||
continue
|
||||
}
|
||||
if strings.ToLower(strings.TrimSpace(fmt.Sprintf("%v", row["status"]))) != "error" {
|
||||
continue
|
||||
}
|
||||
logText := strings.TrimSpace(fmt.Sprintf("%v", row["log"]))
|
||||
if logText == "" {
|
||||
continue
|
||||
}
|
||||
preview := strings.TrimSpace(fmt.Sprintf("%v", row["input_preview"]))
|
||||
score := overlapScore(kw, tokenizeTaskText(preview))
|
||||
if score < 1 || score < bestScore {
|
||||
continue
|
||||
}
|
||||
bestScore = score
|
||||
best = taskAuditErrorEvent{
|
||||
TaskID: strings.TrimSpace(fmt.Sprintf("%v", row["task_id"])),
|
||||
Source: strings.TrimSpace(fmt.Sprintf("%v", row["source"])),
|
||||
Channel: strings.TrimSpace(fmt.Sprintf("%v", row["channel"])),
|
||||
Log: logText,
|
||||
Preview: preview,
|
||||
}
|
||||
}
|
||||
if bestScore == 0 || strings.TrimSpace(best.TaskID) == "" {
|
||||
return taskAuditErrorEvent{}, false
|
||||
}
|
||||
return best, true
|
||||
}
|
||||
|
||||
func tokenizeTaskText(s string) []string {
|
||||
normalized := strings.NewReplacer("\n", " ", "\t", " ", ",", " ", ",", " ", ".", " ", "。", " ", ":", " ", ":", " ", ";", " ", ";", " ").Replace(strings.ToLower(strings.TrimSpace(s)))
|
||||
parts := strings.Fields(normalized)
|
||||
out := make([]string, 0, len(parts))
|
||||
for _, p := range parts {
|
||||
if len(p) < 3 {
|
||||
continue
|
||||
}
|
||||
out = append(out, p)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func overlapScore(a, b []string) int {
|
||||
if len(a) == 0 || len(b) == 0 {
|
||||
return 0
|
||||
}
|
||||
set := make(map[string]struct{}, len(a))
|
||||
for _, k := range a {
|
||||
set[k] = struct{}{}
|
||||
}
|
||||
score := 0
|
||||
for _, k := range b {
|
||||
if _, ok := set[k]; ok {
|
||||
score++
|
||||
}
|
||||
}
|
||||
return score
|
||||
}
|
||||
|
||||
func cloneMetadata(m map[string]string) map[string]string {
|
||||
if len(m) == 0 {
|
||||
return nil
|
||||
|
||||
@@ -2,9 +2,12 @@ package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"clawgo/pkg/bus"
|
||||
"clawgo/pkg/ekg"
|
||||
"clawgo/pkg/providers"
|
||||
)
|
||||
|
||||
@@ -50,3 +53,27 @@ func TestProcessPlannedMessage_AggregatesResults(t *testing.T) {
|
||||
t.Fatalf("expected aggregate response")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindRecentRelatedErrorEvent(t *testing.T) {
|
||||
ws := filepath.Join(t.TempDir(), "workspace")
|
||||
_ = os.MkdirAll(filepath.Join(ws, "memory"), 0o755)
|
||||
line := `{"task_id":"t1","status":"error","log":"open /tmp/a.go failed","input_preview":"修复 pkg/a.go 的读取错误","source":"direct","channel":"cli"}`
|
||||
if err := os.WriteFile(filepath.Join(ws, "memory", "task-audit.jsonl"), []byte(line+"\n"), 0o644); err != nil {
|
||||
t.Fatalf("write audit: %v", err)
|
||||
}
|
||||
loop := &AgentLoop{workspace: ws, ekg: ekg.New(ws)}
|
||||
loop.ekg.Record(ekg.Event{TaskID: "t1", Status: "error", Log: "open /tmp/a.go failed"})
|
||||
loop.ekg.Record(ekg.Event{TaskID: "t1", Status: "error", Log: "open /tmp/a.go failed"})
|
||||
loop.ekg.Record(ekg.Event{TaskID: "t1", Status: "error", Log: "open /tmp/a.go failed"})
|
||||
|
||||
ev, ok := loop.findRecentRelatedErrorEvent("请修复 pkg/a.go 的读取问题")
|
||||
if !ok {
|
||||
t.Fatalf("expected matched recent error event")
|
||||
}
|
||||
if ev.TaskID != "t1" {
|
||||
t.Fatalf("unexpected task id: %s", ev.TaskID)
|
||||
}
|
||||
if hint := loop.ekgHintForTask(plannedTask{Content: "修复 pkg/a.go"}); hint == "" {
|
||||
t.Fatalf("expected non-empty ekg hint")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ type telegramRenderedChunk struct {
|
||||
|
||||
func (c *TelegramChannel) SupportsAction(action string) bool {
|
||||
switch strings.ToLower(strings.TrimSpace(action)) {
|
||||
case "", "send", "edit", "delete", "react", "stream":
|
||||
case "", "send", "edit", "delete", "react", "stream", "finalize":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
@@ -866,6 +866,8 @@ func renderTelegramStreamChunks(content string) []telegramRenderedChunk {
|
||||
parts = splitTelegramMarkdown(body, telegramStreamSplitMaxRunes)
|
||||
case telego.ModeHTML:
|
||||
parts = splitTelegramText(body, telegramSafeHTMLMaxRunes)
|
||||
case "text":
|
||||
parts = splitTelegramText(body, telegramStreamSplitMaxRunes)
|
||||
default:
|
||||
parts = splitTelegramMarkdown(body, telegramStreamSplitMaxRunes)
|
||||
mode = "auto_markdown"
|
||||
@@ -894,6 +896,14 @@ func renderTelegramStreamChunks(content string) []telegramRenderedChunk {
|
||||
if strings.TrimSpace(payload) != "" {
|
||||
out = append(out, telegramRenderedChunk{payload: payload, parseMode: telego.ModeMarkdownV2})
|
||||
}
|
||||
case "text":
|
||||
payload := trimmed
|
||||
if len([]rune(payload)) > telegramStreamSplitMaxRunes {
|
||||
payload = splitTelegramText(payload, telegramStreamSplitMaxRunes)[0]
|
||||
}
|
||||
if strings.TrimSpace(payload) != "" {
|
||||
out = append(out, telegramRenderedChunk{payload: payload, parseMode: ""})
|
||||
}
|
||||
default:
|
||||
payload := sanitizeTelegramHTML(markdownToTelegramHTML(trimmed))
|
||||
if len([]rune(payload)) > telegramSafeHTMLMaxRunes {
|
||||
@@ -914,6 +924,8 @@ func detectTelegramStreamMode(content string) (mode string, body string) {
|
||||
return telego.ModeHTML, strings.TrimSpace(trimmed[len("[mode:html]"):])
|
||||
case strings.HasPrefix(strings.ToLower(trimmed), "[mode:markdownv2]"):
|
||||
return telego.ModeMarkdownV2, strings.TrimSpace(trimmed[len("[mode:markdownv2]"):])
|
||||
case strings.HasPrefix(strings.ToLower(trimmed), "[mode:text]"):
|
||||
return "text", strings.TrimSpace(trimmed[len("[mode:text]"):])
|
||||
default:
|
||||
return "auto_markdown", content
|
||||
}
|
||||
@@ -1056,7 +1068,7 @@ func (c *TelegramChannel) deleteTelegramMessageWithRetry(ctx context.Context, ch
|
||||
|
||||
func (c *TelegramChannel) handleAction(ctx context.Context, chatID int64, action string, msg bus.OutboundMessage) error {
|
||||
messageID, ok := parseTelegramMessageID(msg.MessageID)
|
||||
if !ok && action != "send" && action != "stream" {
|
||||
if !ok && action != "send" && action != "stream" && action != "finalize" {
|
||||
return fmt.Errorf("message_id required for action=%s", action)
|
||||
}
|
||||
switch action {
|
||||
@@ -1068,6 +1080,23 @@ func (c *TelegramChannel) handleAction(ctx context.Context, chatID int64, action
|
||||
return err
|
||||
case "stream":
|
||||
return c.handleStreamAction(ctx, chatID, msg)
|
||||
case "finalize":
|
||||
if strings.TrimSpace(msg.Content) != "" {
|
||||
// Final pass in auto-markdown mode to recover rich formatting after plain streaming.
|
||||
if err := c.handleStreamAction(ctx, chatID, bus.OutboundMessage{
|
||||
ChatID: msg.ChatID,
|
||||
ReplyToID: msg.ReplyToID,
|
||||
Content: msg.Content,
|
||||
Action: "stream",
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
streamKey := telegramStreamKey(chatID, msg.ReplyToID)
|
||||
c.streamMu.Lock()
|
||||
delete(c.streamState, streamKey)
|
||||
c.streamMu.Unlock()
|
||||
return nil
|
||||
case "delete":
|
||||
delCtx, cancel := withTelegramAPITimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
@@ -221,6 +221,24 @@ const resources = {
|
||||
inputPreview: 'Input Preview',
|
||||
blockReason: 'Block Reason',
|
||||
actionFailed: 'Action failed',
|
||||
cronDeleteConfirmTitle: 'Delete Cron Job',
|
||||
cronDeleteConfirmMessage: 'This will permanently delete the cron job. Continue?',
|
||||
cronDisableConfirmTitle: 'Pause Cron Job',
|
||||
cronDisableConfirmMessage: 'Pause this cron job now?',
|
||||
memoryDeleteConfirmTitle: 'Delete Memory File',
|
||||
memoryDeleteConfirmMessage: 'Delete memory file "{{path}}" permanently?',
|
||||
taskDeleteConfirmTitle: 'Delete Task',
|
||||
taskDeleteConfirmMessage: 'Delete task "{{id}}" permanently?',
|
||||
logsClearConfirmTitle: 'Clear Logs',
|
||||
logsClearConfirmMessage: 'Clear current log list from this page?',
|
||||
configDeleteProviderConfirmTitle: 'Delete Provider',
|
||||
configDeleteProviderConfirmMessage: 'Delete provider "{{name}}" from current config?',
|
||||
taskPauseConfirmTitle: 'Pause Task',
|
||||
taskPauseConfirmMessage: 'Pause task "{{id}}" now?',
|
||||
taskCompleteConfirmTitle: 'Complete Task',
|
||||
taskCompleteConfirmMessage: 'Mark task "{{id}}" as completed?',
|
||||
taskIgnoreConfirmTitle: 'Ignore Task',
|
||||
taskIgnoreConfirmMessage: 'Ignore task "{{id}}"? This may hide follow-up processing.',
|
||||
cronExpressionPlaceholder: '*/5 * * * *',
|
||||
recipientId: 'recipient id',
|
||||
languageZh: '中文',
|
||||
@@ -643,6 +661,24 @@ const resources = {
|
||||
inputPreview: '输入预览',
|
||||
blockReason: '阻断原因',
|
||||
actionFailed: '操作失败',
|
||||
cronDeleteConfirmTitle: '删除定时任务',
|
||||
cronDeleteConfirmMessage: '此操作会永久删除该定时任务,是否继续?',
|
||||
cronDisableConfirmTitle: '暂停定时任务',
|
||||
cronDisableConfirmMessage: '确认暂停该定时任务吗?',
|
||||
memoryDeleteConfirmTitle: '删除记忆文件',
|
||||
memoryDeleteConfirmMessage: '确认永久删除记忆文件“{{path}}”吗?',
|
||||
taskDeleteConfirmTitle: '删除任务',
|
||||
taskDeleteConfirmMessage: '确认永久删除任务“{{id}}”吗?',
|
||||
logsClearConfirmTitle: '清空日志',
|
||||
logsClearConfirmMessage: '确认清空当前页面中的日志列表吗?',
|
||||
configDeleteProviderConfirmTitle: '删除 Provider',
|
||||
configDeleteProviderConfirmMessage: '确认从当前配置中删除 provider “{{name}}”吗?',
|
||||
taskPauseConfirmTitle: '暂停任务',
|
||||
taskPauseConfirmMessage: '确认暂停任务“{{id}}”吗?',
|
||||
taskCompleteConfirmTitle: '完成任务',
|
||||
taskCompleteConfirmMessage: '确认将任务“{{id}}”标记为完成吗?',
|
||||
taskIgnoreConfirmTitle: '忽略任务',
|
||||
taskIgnoreConfirmMessage: '确认忽略任务“{{id}}”吗?这可能会跳过后续处理。',
|
||||
cronExpressionPlaceholder: '*/5 * * * *',
|
||||
recipientId: '接收者 ID',
|
||||
languageZh: '中文',
|
||||
|
||||
@@ -2,6 +2,7 @@ import React, { useEffect, useMemo, useState } from 'react';
|
||||
import { RefreshCw, Save } from 'lucide-react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useAppContext } from '../context/AppContext';
|
||||
import { useUI } from '../context/UIContext';
|
||||
import RecursiveConfig from '../components/RecursiveConfig';
|
||||
|
||||
function setPath(obj: any, path: string, value: any) {
|
||||
@@ -19,6 +20,7 @@ function setPath(obj: any, path: string, value: any) {
|
||||
|
||||
const Config: React.FC = () => {
|
||||
const { t } = useTranslation();
|
||||
const ui = useUI();
|
||||
const { cfg, setCfg, cfgRaw, setCfgRaw, loadConfig, hotReloadFieldDetails, q } = useAppContext();
|
||||
const [showRaw, setShowRaw] = useState(false);
|
||||
const [basicMode, setBasicMode] = useState(true);
|
||||
@@ -94,7 +96,14 @@ const Config: React.FC = () => {
|
||||
setCfg((v) => setPath(v, `providers.proxies.${name}.${field}`, value));
|
||||
}
|
||||
|
||||
function removeProxy(name: string) {
|
||||
async function removeProxy(name: string) {
|
||||
const ok = await ui.confirmDialog({
|
||||
title: t('configDeleteProviderConfirmTitle'),
|
||||
message: t('configDeleteProviderConfirmMessage', { name }),
|
||||
danger: true,
|
||||
confirmText: t('delete'),
|
||||
});
|
||||
if (!ok) return;
|
||||
setCfg((v) => {
|
||||
const next = JSON.parse(JSON.stringify(v || {}));
|
||||
if (next?.providers?.proxies && typeof next.providers.proxies === 'object') {
|
||||
|
||||
@@ -3,7 +3,9 @@ import { Plus, RefreshCw, CheckCircle2, Pause, Edit2, Trash2, X, Play, Clock } f
|
||||
import { motion, AnimatePresence } from 'motion/react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useAppContext } from '../context/AppContext';
|
||||
import { useUI } from '../context/UIContext';
|
||||
import { CronJob } from '../types';
|
||||
import { formatLocalDateTime } from '../utils/time';
|
||||
|
||||
const initialCronForm = {
|
||||
name: '',
|
||||
@@ -37,7 +39,7 @@ const formatSchedule = (job: CronJob, t: (key: string) => string) => {
|
||||
if (kind === 'at' && job.schedule?.atMs) {
|
||||
return {
|
||||
label: t('runAt'),
|
||||
value: new Date(job.schedule.atMs).toLocaleString(),
|
||||
value: formatLocalDateTime(job.schedule.atMs),
|
||||
};
|
||||
}
|
||||
return {
|
||||
@@ -48,6 +50,7 @@ const formatSchedule = (job: CronJob, t: (key: string) => string) => {
|
||||
|
||||
const Cron: React.FC = () => {
|
||||
const { t } = useTranslation();
|
||||
const ui = useUI();
|
||||
const { cron, refreshCron, q, cfg } = useAppContext();
|
||||
const [isCronModalOpen, setIsCronModalOpen] = useState(false);
|
||||
const [editingCron, setEditingCron] = useState<CronJob | null>(null);
|
||||
@@ -72,6 +75,23 @@ const Cron: React.FC = () => {
|
||||
}, [cfg, enabledChannels]);
|
||||
|
||||
async function cronAction(action: 'delete' | 'enable' | 'disable', id: string) {
|
||||
if (action === 'delete') {
|
||||
const ok = await ui.confirmDialog({
|
||||
title: t('cronDeleteConfirmTitle'),
|
||||
message: t('cronDeleteConfirmMessage'),
|
||||
danger: true,
|
||||
confirmText: t('delete'),
|
||||
});
|
||||
if (!ok) return;
|
||||
}
|
||||
if (action === 'disable') {
|
||||
const ok = await ui.confirmDialog({
|
||||
title: t('cronDisableConfirmTitle'),
|
||||
message: t('cronDisableConfirmMessage'),
|
||||
confirmText: t('pause'),
|
||||
});
|
||||
if (!ok) return;
|
||||
}
|
||||
try {
|
||||
await fetch(`/webui/api/cron${q}`, {
|
||||
method: 'POST',
|
||||
|
||||
@@ -2,10 +2,13 @@ import React, { useEffect, useState, useRef } from 'react';
|
||||
import { Terminal, Trash2, Play, Square } from 'lucide-react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useAppContext } from '../context/AppContext';
|
||||
import { useUI } from '../context/UIContext';
|
||||
import { LogEntry } from '../types';
|
||||
import { formatLocalTime } from '../utils/time';
|
||||
|
||||
const Logs: React.FC = () => {
|
||||
const { t } = useTranslation();
|
||||
const ui = useUI();
|
||||
const { q } = useAppContext();
|
||||
const [logs, setLogs] = useState<LogEntry[]>([]);
|
||||
const [codeMap, setCodeMap] = useState<Record<number, string>>({});
|
||||
@@ -109,7 +112,16 @@ const Logs: React.FC = () => {
|
||||
logEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
||||
}, [logs]);
|
||||
|
||||
const clearLogs = () => setLogs([]);
|
||||
const clearLogs = async () => {
|
||||
const ok = await ui.confirmDialog({
|
||||
title: t('logsClearConfirmTitle'),
|
||||
message: t('logsClearConfirmMessage'),
|
||||
danger: true,
|
||||
confirmText: t('clear'),
|
||||
});
|
||||
if (!ok) return;
|
||||
setLogs([]);
|
||||
};
|
||||
|
||||
const normalizeLog = (v: any): LogEntry => ({
|
||||
time: typeof v?.time === 'string' && v.time ? v.time : (typeof v?.timestamp === 'string' && v.timestamp ? v.timestamp : new Date().toISOString()),
|
||||
@@ -135,19 +147,6 @@ const Logs: React.FC = () => {
|
||||
return codeMap[c] || v;
|
||||
};
|
||||
|
||||
const formatTime = (raw: string) => {
|
||||
try {
|
||||
if (!raw) return '--:--:--';
|
||||
if (raw.includes('T')) {
|
||||
const right = raw.split('T')[1] || '';
|
||||
return (right.split('.')[0] || right).trim() || '--:--:--';
|
||||
}
|
||||
return raw;
|
||||
} catch {
|
||||
return '--:--:--';
|
||||
}
|
||||
};
|
||||
|
||||
const renderReadable = (log: LogEntry) => {
|
||||
const keys = Object.keys(log).filter(k => !['time', 'level', 'msg', '__raw'].includes(k));
|
||||
const core = `${log.msg}`;
|
||||
@@ -241,7 +240,7 @@ const Logs: React.FC = () => {
|
||||
const code = toCode(rawCode);
|
||||
return (
|
||||
<tr key={i} className="border-b border-zinc-900 hover:bg-zinc-900/40 align-top">
|
||||
<td className="p-2 text-zinc-500 whitespace-nowrap">{formatTime(log.time)}</td>
|
||||
<td className="p-2 text-zinc-500 whitespace-nowrap">{formatLocalTime(log.time)}</td>
|
||||
<td className={`p-2 font-semibold whitespace-nowrap ${getLevelColor(lvl)}`}>{lvl}</td>
|
||||
<td className="p-2 text-zinc-200 break-all">{message}</td>
|
||||
<td className="p-2 text-red-300 break-all">{errText}</td>
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useAppContext } from '../context/AppContext';
|
||||
import { useUI } from '../context/UIContext';
|
||||
|
||||
const Memory: React.FC = () => {
|
||||
const { t } = useTranslation();
|
||||
const ui = useUI();
|
||||
const { q } = useAppContext();
|
||||
const [files, setFiles] = useState<string[]>([]);
|
||||
const [active, setActive] = useState('');
|
||||
@@ -35,6 +37,13 @@ const Memory: React.FC = () => {
|
||||
}
|
||||
|
||||
async function removeFile(path: string) {
|
||||
const ok = await ui.confirmDialog({
|
||||
title: t('memoryDeleteConfirmTitle'),
|
||||
message: t('memoryDeleteConfirmMessage', { path }),
|
||||
danger: true,
|
||||
confirmText: t('delete'),
|
||||
});
|
||||
if (!ok) return;
|
||||
await fetch(`/webui/api/memory${qp('path', path)}`, { method: 'DELETE' });
|
||||
if (active === path) {
|
||||
setActive('');
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import React, { useEffect, useMemo, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useAppContext } from '../context/AppContext';
|
||||
import { useUI } from '../context/UIContext';
|
||||
import { formatLocalDateTime, localDateInputValue } from '../utils/time';
|
||||
|
||||
type TaskAuditItem = {
|
||||
task_id?: string;
|
||||
@@ -29,6 +31,7 @@ type TaskAuditItem = {
|
||||
|
||||
const TaskAudit: React.FC = () => {
|
||||
const { t } = useTranslation();
|
||||
const ui = useUI();
|
||||
const { q } = useAppContext();
|
||||
const [items, setItems] = useState<TaskAuditItem[]>([]);
|
||||
const [selected, setSelected] = useState<TaskAuditItem | null>(null);
|
||||
@@ -36,7 +39,7 @@ const TaskAudit: React.FC = () => {
|
||||
const [sourceFilter, setSourceFilter] = useState('all');
|
||||
const [statusFilter, setStatusFilter] = useState('all');
|
||||
const [dailyReport, setDailyReport] = useState<string>('');
|
||||
const [reportDate, setReportDate] = useState<string>(new Date().toISOString().slice(0,10));
|
||||
const [reportDate, setReportDate] = useState<string>(localDateInputValue());
|
||||
const [showDailyReport, setShowDailyReport] = useState(false);
|
||||
|
||||
const fetchData = async () => {
|
||||
@@ -90,6 +93,31 @@ const TaskAudit: React.FC = () => {
|
||||
|
||||
const taskAction = async (action: 'pause'|'retry'|'complete'|'ignore') => {
|
||||
if (!selected?.task_id) return;
|
||||
if (action === 'pause') {
|
||||
const ok = await ui.confirmDialog({
|
||||
title: t('taskPauseConfirmTitle'),
|
||||
message: t('taskPauseConfirmMessage', { id: selected.task_id }),
|
||||
confirmText: t('pauseTask'),
|
||||
});
|
||||
if (!ok) return;
|
||||
}
|
||||
if (action === 'complete') {
|
||||
const ok = await ui.confirmDialog({
|
||||
title: t('taskCompleteConfirmTitle'),
|
||||
message: t('taskCompleteConfirmMessage', { id: selected.task_id }),
|
||||
confirmText: t('completeTask'),
|
||||
});
|
||||
if (!ok) return;
|
||||
}
|
||||
if (action === 'ignore') {
|
||||
const ok = await ui.confirmDialog({
|
||||
title: t('taskIgnoreConfirmTitle'),
|
||||
message: t('taskIgnoreConfirmMessage', { id: selected.task_id }),
|
||||
danger: true,
|
||||
confirmText: t('ignoreTask'),
|
||||
});
|
||||
if (!ok) return;
|
||||
}
|
||||
try {
|
||||
const url = `/webui/api/task_queue${q}`;
|
||||
const r = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action, task_id: selected.task_id }) });
|
||||
@@ -157,7 +185,7 @@ const TaskAudit: React.FC = () => {
|
||||
>
|
||||
<div className="text-sm font-medium text-zinc-100 truncate">{it.task_id || `task-${idx + 1}`}</div>
|
||||
<div className="text-xs text-zinc-400 truncate">{it.channel || '-'} · {it.status} · attempts:{it.attempts || 1} · {it.duration_ms || 0}ms · retry:{it.retry_count || 0} · {it.source || '-'} · {it.provider || '-'} / {it.model || '-'}</div>
|
||||
<div className="text-[11px] text-zinc-500 truncate">{it.time}</div>
|
||||
<div className="text-[11px] text-zinc-500 truncate">{formatLocalDateTime(it.time)}</div>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
@@ -188,7 +216,7 @@ const TaskAudit: React.FC = () => {
|
||||
<div><div className="text-zinc-500 text-xs">{t('session')}</div><div className="font-mono break-all">{selected.session}</div></div>
|
||||
<div><div className="text-zinc-500 text-xs">{t('provider')}</div><div>{selected.provider || '-'}</div></div>
|
||||
<div><div className="text-zinc-500 text-xs">{t('model')}</div><div>{selected.model || '-'}</div></div>
|
||||
<div><div className="text-zinc-500 text-xs">{t('time')}</div><div>{selected.time}</div></div>
|
||||
<div><div className="text-zinc-500 text-xs">{t('time')}</div><div>{formatLocalDateTime(selected.time)}</div></div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
@@ -213,7 +241,7 @@ const TaskAudit: React.FC = () => {
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-zinc-500 text-xs mb-1">{t('lastPauseAt')}</div>
|
||||
<div className="p-2 rounded bg-zinc-950/60 border border-zinc-800 whitespace-pre-wrap text-zinc-200">{selected.last_pause_at || '-'}</div>
|
||||
<div className="p-2 rounded bg-zinc-950/60 border border-zinc-800 whitespace-pre-wrap text-zinc-200">{formatLocalDateTime(selected.last_pause_at)}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useAppContext } from '../context/AppContext';
|
||||
import { useUI } from '../context/UIContext';
|
||||
|
||||
type TaskItem = {
|
||||
id?: string;
|
||||
@@ -14,6 +15,7 @@ type TaskItem = {
|
||||
|
||||
const Tasks: React.FC = () => {
|
||||
const { t } = useTranslation();
|
||||
const ui = useUI();
|
||||
const { q } = useAppContext();
|
||||
const [items, setItems] = useState<TaskItem[]>([]);
|
||||
const [selected, setSelected] = useState<TaskItem | null>(null);
|
||||
@@ -34,6 +36,17 @@ const Tasks: React.FC = () => {
|
||||
useEffect(() => { load(); }, [q]);
|
||||
|
||||
const save = async (action: 'create' | 'update' | 'delete') => {
|
||||
if (action === 'delete') {
|
||||
const targetId = String(draft.id || '').trim();
|
||||
if (!targetId) return;
|
||||
const ok = await ui.confirmDialog({
|
||||
title: t('taskDeleteConfirmTitle'),
|
||||
message: t('taskDeleteConfirmMessage', { id: targetId }),
|
||||
danger: true,
|
||||
confirmText: t('delete'),
|
||||
});
|
||||
if (!ok) return;
|
||||
}
|
||||
const payload: any = { action };
|
||||
if (action === 'create') payload.item = draft;
|
||||
if (action === 'update') { payload.id = draft.id; payload.item = { id: draft.id, content: draft.content }; }
|
||||
|
||||
49
webui/src/utils/time.ts
Normal file
49
webui/src/utils/time.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
type DateLike = string | number | Date | null | undefined;
|
||||
|
||||
function parseDateLike(value: DateLike): Date | null {
|
||||
if (value == null) return null;
|
||||
if (value instanceof Date) {
|
||||
return Number.isNaN(value.getTime()) ? null : value;
|
||||
}
|
||||
if (typeof value === 'number') {
|
||||
const d = new Date(value);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
const s = String(value).trim();
|
||||
if (!s) return null;
|
||||
|
||||
if (/^\d+$/.test(s)) {
|
||||
const n = Number(s);
|
||||
const ms = n > 1e12 ? n : n * 1000;
|
||||
const d = new Date(ms);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
const d = new Date(s);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
export function formatLocalDateTime(value: DateLike, fallback = '-'): string {
|
||||
const d = parseDateLike(value);
|
||||
if (!d) {
|
||||
const raw = value == null ? '' : String(value).trim();
|
||||
return raw || fallback;
|
||||
}
|
||||
return d.toLocaleString();
|
||||
}
|
||||
|
||||
export function formatLocalTime(value: DateLike, fallback = '--:--:--'): string {
|
||||
const d = parseDateLike(value);
|
||||
if (!d) {
|
||||
const raw = value == null ? '' : String(value).trim();
|
||||
return raw || fallback;
|
||||
}
|
||||
return d.toLocaleTimeString();
|
||||
}
|
||||
|
||||
export function localDateInputValue(base = new Date()): string {
|
||||
const d = new Date(base.getTime());
|
||||
d.setMinutes(d.getMinutes() - d.getTimezoneOffset());
|
||||
return d.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user