Files
Cyrene/backend/ai-core/internal/host/backend_docker.go
T
AskaEth 91c9ee4b2d fix: 修复 AI 回复无法送达发送者 + 重复消息 + action角色泄露 + OS环境支持
广播逻辑重构:
- AI 回复 (stream_start/response/stream_segments/multi_message/stream_end) 改用 broadcastToUser 发送给所有客户端
- 用户消息回显保持 broadcastToUserExcept 排除发送者

消息去重与角色修复:
- CacheMessage(user) 移至回复生成后,避免本轮 LLM 调用出现重复用户消息
- action 角色消息在 DB 存储时映射为 assistant,DeepSeek 等模型不支持自定义角色
- stream_end defer 机制确保错误路径也会终止客户端思考指示器

OS 完整环境支持:
- host 包重构为 HostBackend 接口 + Direct/WSL/Docker 三种后端
- 新增 os_exec/os_file/os_system 工具供 AI 在完整 Linux 环境中自由操作

其他:
- 视觉模型注入 + 图片预处理后清空 Images 避免传给 Chat 模型
- 图片 URL 相对路径→绝对 URL 转换
- DevTools 链路追踪页面 + 重启修复
- 记忆搜索模糊匹配增强
- 后台思考定时调度支持
- 管理后台页面 (模型配置/用户管理等)
- docs/api 更新广播机制说明

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-29 12:46:17 +08:00

275 lines
7.7 KiB
Go

package host
import (
"bytes"
"context"
"fmt"
"os/exec"
"strings"
"time"
)
// DockerBackend executes commands inside a Docker container,
// providing a full Linux OS environment with container-level isolation.
type DockerBackend struct {
container string
image string
timeout time.Duration
}
// NewDockerBackend creates a Docker backend that runs commands in the
// specified container. If the container does not exist, it will be
// created from the given image.
func NewDockerBackend(container, image string, defaultTimeout time.Duration) *DockerBackend {
if defaultTimeout <= 0 {
defaultTimeout = 30 * time.Second
}
return &DockerBackend{
container: container,
image: image,
timeout: defaultTimeout,
}
}
func (b *DockerBackend) Name() string { return "docker" }
// ensureContainer checks that the container exists and is running.
// If it doesn't exist, it creates it from the configured image.
func (b *DockerBackend) ensureContainer() error {
// Check if container exists and is running
check := exec.Command("docker", "inspect", "-f", "{{.State.Running}}", b.container)
out, err := check.Output()
if err == nil && strings.TrimSpace(string(out)) == "true" {
return nil
}
// Check if container exists but is stopped
if err == nil && strings.TrimSpace(string(out)) == "false" {
start := exec.Command("docker", "start", b.container)
if out, err := start.CombinedOutput(); err != nil {
return fmt.Errorf("cannot start container %s: %s — %w", b.container, string(out), err)
}
return nil
}
// Create and start a new container
create := exec.Command("docker", "run", "-d", "--name", b.container,
"--restart", "unless-stopped",
b.image, "sleep", "infinity")
if out, err := create.CombinedOutput(); err != nil {
return fmt.Errorf("cannot create container %s from image %s: %s — %w",
b.container, b.image, string(out), err)
}
return nil
}
// Exec runs a command inside the Docker container.
func (b *DockerBackend) Exec(ctx context.Context, command, workDir string, timeout time.Duration) (*ExecResult, error) {
if command == "" {
return nil, fmt.Errorf("empty command")
}
if err := b.ensureContainer(); err != nil {
return nil, err
}
if timeout <= 0 {
timeout = b.timeout
}
execCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
// Build the shell command to run inside the container
script := command
if workDir != "" {
script = fmt.Sprintf("cd %s && %s", shellEscapeDocker(workDir), command)
}
cmd := exec.CommandContext(execCtx, "docker", "exec", b.container, "sh", "-c", script)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
start := time.Now()
err := cmd.Run()
elapsed := time.Since(start)
result := &ExecResult{
Duration: elapsed.Round(time.Millisecond).String(),
Stdout: stdout.String(),
Stderr: stderr.String(),
}
if execCtx.Err() == context.DeadlineExceeded {
result.TimedOut = true
result.ExitCode = -1
return result, fmt.Errorf("command timed out after %s", timeout)
}
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
result.ExitCode = exitErr.ExitCode()
} else {
result.ExitCode = -1
}
} else {
result.ExitCode = 0
}
return result, err
}
// ReadFile reads a file from inside the container using cat.
func (b *DockerBackend) ReadFile(path string, maxBytes int) (string, error) {
if maxBytes <= 0 {
maxBytes = 1024 * 1024
}
if err := b.ensureContainer(); err != nil {
return "", err
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "docker", "exec", b.container, "cat", path)
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("cannot read file %s: %w", path, err)
}
if len(out) > maxBytes {
out = out[:maxBytes]
}
return string(out), nil
}
// WriteFile writes content to a file inside the container.
func (b *DockerBackend) WriteFile(path, content string, maxBytes int) error {
if maxBytes <= 0 {
maxBytes = 1024 * 1024
}
if len(content) > maxBytes {
return fmt.Errorf("content too large: %d bytes (max %d)", len(content), maxBytes)
}
if err := b.ensureContainer(); err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Create parent directory and write file
cmd := exec.CommandContext(ctx, "docker", "exec", "-i", b.container, "sh", "-c",
fmt.Sprintf("mkdir -p $(dirname %s) && cat > %s", shellEscapeDocker(path), shellEscapeDocker(path)))
cmd.Stdin = strings.NewReader(content)
_, err := cmd.Output()
if err != nil {
return fmt.Errorf("cannot write file %s: %w", path, err)
}
return nil
}
// ListDir lists a directory inside the container.
func (b *DockerBackend) ListDir(path string) ([]DirEntry, error) {
if err := b.ensureContainer(); err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "docker", "exec", b.container, "sh", "-c",
fmt.Sprintf("ls -la %s 2>/dev/null | tail -n +2 || echo ''", shellEscapeDocker(path)))
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("cannot list dir %s: %w", path, err)
}
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
result := make([]DirEntry, 0, len(lines))
for _, line := range lines {
if line == "" || strings.HasPrefix(line, "total ") {
continue
}
// Parse ls -la output: drwxr-xr-x 2 root root 4096 Jan 1 12:00 name
fields := strings.Fields(line)
if len(fields) < 9 {
continue
}
isDir := strings.HasPrefix(fields[0], "d")
name := fields[len(fields)-1]
if name == "." || name == ".." {
continue
}
var size int64
fmt.Sscanf(fields[4], "%d", &size)
result = append(result, DirEntry{
Name: name,
IsDir: isDir,
Size: size,
})
}
return result, nil
}
// SystemInfo returns system information from inside the container.
func (b *DockerBackend) SystemInfo() map[string]interface{} {
info := map[string]interface{}{
"backend": "docker",
"container": b.container,
"image": b.image,
}
if err := b.ensureContainer(); err != nil {
info["error"] = err.Error()
return info
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if out, err := exec.CommandContext(ctx, "docker", "exec", b.container, "uname", "-a").Output(); err == nil {
info["uname"] = strings.TrimSpace(string(out))
}
if out, err := exec.CommandContext(ctx, "docker", "exec", b.container, "hostname").Output(); err == nil {
info["hostname"] = strings.TrimSpace(string(out))
}
if out, err := exec.CommandContext(ctx, "docker", "exec", b.container, "free", "-h").Output(); err == nil {
info["memory"] = strings.TrimSpace(string(out))
}
if out, err := exec.CommandContext(ctx, "docker", "exec", b.container, "df", "-h", "/").Output(); err == nil {
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
if len(lines) > 1 {
info["disk"] = strings.TrimSpace(lines[1])
}
}
return info
}
// DiskUsage returns disk usage for a path inside the container.
func (b *DockerBackend) DiskUsage(path string) (map[string]interface{}, error) {
if err := b.ensureContainer(); err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "docker", "exec", b.container, "stat", path)
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("cannot stat path %s: %w", path, err)
}
return map[string]interface{}{
"path": path,
"stat": strings.TrimSpace(string(out)),
}, nil
}
// shellEscapeDocker escapes a string for safe use in a shell command.
func shellEscapeDocker(s string) string {
escaped := strings.ReplaceAll(s, "'", "'\\''")
return "'" + escaped + "'"
}