mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-02 12:01:50 +08:00
- Implement full-traffic transparent gateway (cmd/artex-gateway, traffic/netstack.go, traffic/transparent.go) using Linux TUN + gVisor netstack deferred TCP handshakes (eliminating nmap/nc false-positive open ports), real ICMPv4 Echo proxying, UDP/DNS interception, and per-container upstream proxy overrides. - Add egress blacklist + asset-intercept sync, non-HTTP flow/DNS SQLite persistence and APIs, and /system/proxy & /function/traffic TCP/UDP/DNS UI panels. - Fix sandbox Playwright MCP browser loopback --proxy-server rewrite and NODE_EXTRA_CA_CERTS override in server/mcpstdio.go. - Strip all SUID/SGID binaries in Dockerfile.sandbox, grant minimal cap_net_raw+ep to nmap/nping/ping with ARTEX_SANDBOX_ALLOW_NET_RAW toggle, and enforce IPv4/IPv6 anti-spoofing in iptables and netstack. - Remove hardcoded Chinese "(全局)" from GET /api/proxy/routes, localize ContainerRoutesTab / NonHTTPTrafficPanel labels, and expose reactive locale/isEn from useLiveTranslation for React Compiler compatibility.
233 lines
10 KiB
Go
233 lines
10 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/Autumn-27/artex/db"
|
|
"github.com/Autumn-27/artex/guard"
|
|
"github.com/Autumn-27/artex/intercept"
|
|
"github.com/Autumn-27/artex/internal/sandbox"
|
|
"github.com/Autumn-27/artex/traffic"
|
|
"github.com/Autumn-27/norma/agentcore"
|
|
"github.com/Autumn-27/norma/llm"
|
|
"github.com/Autumn-27/norma/permission"
|
|
actool "github.com/Autumn-27/norma/tool"
|
|
"github.com/Autumn-27/norma/transcript"
|
|
)
|
|
|
|
// ChatAgent is the generic, task-independent conversational runner behind the chat
|
|
// page. It generalizes MainAgent.Chat: any agent (built-in OR a custom one, by
|
|
// key) can be chatted with, multi-turn history resumed from the transcript. It is
|
|
// a PURE ASSISTANT — base tools are the SDK DefaultTools (Bash/Read/Write/Edit/
|
|
// LS/Glob/Grep) plus whatever skills/MCP the key is made visible; NO pentest
|
|
// graph/task context is injected (that stays exclusive to MainAgent).
|
|
type ChatAgent struct {
|
|
prov llm.Provider
|
|
model string
|
|
workDir string
|
|
tx *transcript.Store
|
|
window int
|
|
proxyAddr string
|
|
proxyAddrFn func() string
|
|
proxyCACert string
|
|
webSearch WebSearchOpts
|
|
guard *guard.Guard // optional; nil disables intercept hooks for chat
|
|
nonStreamingFn func() bool // resolver: use non-streaming (Complete) path? (nil = streaming)
|
|
noaEnabledFn func() bool // resolver: use experimental noa compaction? (nil = off)
|
|
maxTokensFn func() int // resolver: per-reply output cap (nil/0 = send no cap)
|
|
}
|
|
|
|
func NewChatAgent(prov llm.Provider, model, workDir string, tx *transcript.Store, window int) *ChatAgent {
|
|
return &ChatAgent{prov: prov, model: model, workDir: workDir, tx: tx, window: window}
|
|
}
|
|
|
|
// SetNonStreaming wires a resolver deciding whether chat runs use the
|
|
// non-streaming model path (true = non-streaming). nil/unset = streaming.
|
|
func (c *ChatAgent) SetNonStreaming(fn func() bool) { c.nonStreamingFn = fn }
|
|
|
|
func (c *ChatAgent) nonStreaming() bool { return c.nonStreamingFn != nil && c.nonStreamingFn() }
|
|
|
|
// SetNoaEnabled wires a resolver deciding whether chat runs use the experimental
|
|
// noa context-compression mechanism. nil/unset = off (built-in compaction). Read
|
|
// per run so the settings toggle takes effect without rebuilding the agent.
|
|
func (c *ChatAgent) SetNoaEnabled(fn func() bool) { c.noaEnabledFn = fn }
|
|
|
|
// SetMaxTokens wires a resolver for the per-reply output cap. nil/unset or 0 =
|
|
// send no cap and let the endpoint decide. Read per run, like nonStreaming.
|
|
func (c *ChatAgent) SetMaxTokens(fn func() int) { c.maxTokensFn = fn }
|
|
|
|
func (c *ChatAgent) maxTokens() int {
|
|
if c.maxTokensFn == nil {
|
|
return 0
|
|
}
|
|
return c.maxTokensFn()
|
|
}
|
|
|
|
// SetProxy points the chat agent's WebFetch/Bash at the recording proxy plus the
|
|
// CA cert it trusts (empty addr = direct). Kept for parity with the other agents.
|
|
func (c *ChatAgent) SetProxy(addr, caCert string) { c.proxyAddr, c.proxyCACert = addr, caCert }
|
|
func (c *ChatAgent) SetProxyProvider(fn func() string) { c.proxyAddrFn = fn }
|
|
|
|
// SetWebSearch selects the web_search backend for the chat agent (off by default).
|
|
func (c *ChatAgent) SetWebSearch(o WebSearchOpts) { c.webSearch = o }
|
|
|
|
// SetGuard attaches a guard (with user-configured intercept rules) to this chat
|
|
// agent. Must be called before Chat; safe to call multiple times.
|
|
func (c *ChatAgent) SetGuard(g *guard.Guard) { c.guard = g }
|
|
|
|
// chatWorkDirSpec returns a working-directory notice appended to every chat
|
|
// agent's system prompt. Mirrors artifactSpec but without pentest-specific
|
|
// wording ("payload", "抓响应体") that would be odd in a general assistant.
|
|
func chatWorkDirSpec(workDir string) string {
|
|
return "\n\n**文件输出规约**:需要写文件时,一律写到工作目录 " + workDir + "(这是默认 CWD,相对路径即落在这里,也可用该绝对路径)——不要写 /tmp 或其他绝对路径。"
|
|
}
|
|
|
|
// chatSystem renders the DB-managed prompt body for agentKey. Custom agents have
|
|
// no per-key in-code default, so DefaultAssistantPrompt is the render fallback.
|
|
func chatSystem(agentKey, dataDir, workDir string) string {
|
|
return renderSystem(agentKey, DefaultAssistantPrompt, chatVars{DataDir: dataDir, Now: nowStr()}) + chatWorkDirSpec(workDir)
|
|
}
|
|
|
|
// chatVars carries the runtime variables a custom agent's prompt may reference.
|
|
// DataDir (server data root) + Now (server wall-clock, refreshed each turn) are the
|
|
// universal ones; any other {{.X}} fails to render and falls back to
|
|
// DefaultAssistantPrompt.
|
|
type chatVars struct{ DataDir, Now string }
|
|
|
|
// Chat runs ONE turn of a conversation with the agent identified by agentKey,
|
|
// resuming prior history keyed by sessionID. maxTurns is the per-turn agent step
|
|
// budget (0 = unlimited). maxDuration is the wall-clock run budget per turn
|
|
// (0 = unlimited); the timer resets each time Chat is called, so a new user
|
|
// message always starts a fresh countdown. webSearch gates network search for
|
|
// THIS agent (the global backend/key still come from the chat agent's config,
|
|
// but each agent decides on/off). emit receives each execution step (thinking /
|
|
// tool_use / tool_result / text / result), tagged with the agent key as the
|
|
// worker lane.
|
|
func (c *ChatAgent) Chat(ctx context.Context, agentKey, sessionID, message string, maxTurns int, maxDuration time.Duration, webSearch bool, emit func(db.Activity)) (string, error) {
|
|
// gate the global web-search opts by this agent's own flag.
|
|
ws := c.webSearch
|
|
if !webSearch {
|
|
ws.Enabled = false
|
|
}
|
|
|
|
sbMgr := sandbox.Default()
|
|
sandboxActive := sbMgr != nil && sbMgr.Enabled()
|
|
sbTarget := sandbox.Target{Scope: sandbox.ScopeSession, OwnerID: sessionID}
|
|
if preset, ok := sandbox.TargetFrom(ctx); ok && preset.Valid() {
|
|
sbTarget = preset
|
|
}
|
|
|
|
// Per-session working directory: <workDir>/sessions/<sessionID>/
|
|
// When bound to a task sandbox (e.g. task-scoped reporter screenshots), use
|
|
// that task's workspace root so screenshots land directly in tasks/<id>/.
|
|
sessionWorkDir := filepath.Join(c.workDir, "sessions", sessionID)
|
|
if sandboxActive && sbTarget.Scope == sandbox.ScopeTask {
|
|
sessionWorkDir = sbMgr.Config().LocalRootFor(sbTarget)
|
|
}
|
|
_ = os.MkdirAll(sessionWorkDir, 0o755)
|
|
ctx = intercept.WithReviewWorkingDirectory(ctx, sessionWorkDir)
|
|
|
|
// Pure assistant: DefaultTools as the base; AugmentTools layers in the key's
|
|
// visible skills/MCP and lets the DB tools table filter/override. DefaultTools
|
|
// have no tools-table rows, so they always pass through.
|
|
base := guardedDefaultTools()
|
|
displayWorkDir := sessionWorkDir
|
|
displayDataDir := c.workDir
|
|
if sandboxActive {
|
|
sbMgr.Acquire(sbTarget)
|
|
defer sbMgr.Release(sbTarget)
|
|
ctx = sandbox.WithTarget(ctx, sbTarget)
|
|
hostRoot := sbMgr.Config().LocalRootFor(sbTarget)
|
|
base = wrapSandboxFileTools(base, hostRoot, "")
|
|
displayWorkDir = sandbox.ContainerWorkDir
|
|
displayDataDir = sandbox.ContainerWorkDir
|
|
}
|
|
ctx = WithRunInfo(ctx, RunInfo{SessionID: sessionID, WorkDir: sessionWorkDir})
|
|
tools, def, cleanup := AugmentTools(ctx, agentKey, base)
|
|
defer cleanup()
|
|
|
|
system, boundary := deferredSystem(chatSystem(agentKey, displayDataDir, displayWorkDir), def)
|
|
proxyAddr := c.proxyAddr
|
|
if c.proxyAddrFn != nil {
|
|
proxyAddr = c.proxyAddrFn()
|
|
}
|
|
srcID := traffic.SourceIdentity{
|
|
Scope: string(sbTarget.Scope),
|
|
OwnerID: sbTarget.OwnerID,
|
|
SessionID: sessionID,
|
|
}
|
|
webFetchProxy := proxyAddr
|
|
if c.proxyCACert != "" && proxyAddr != "" {
|
|
webFetchProxy = traffic.FormatProxyContextURL(proxyAddr, srcID)
|
|
}
|
|
bashEnv := proxyEnv(webFetchProxy, c.proxyCACert)
|
|
if sandboxActive {
|
|
if proxyAddr != "" && c.proxyCACert != "" && sbMgr.Config().ProxyURL != "" {
|
|
bashEnv = sbMgr.ProxyEnvWithURL(traffic.FormatProxyContextURL(sbMgr.Config().ProxyURL, srcID))
|
|
} else {
|
|
bashEnv = sbMgr.ProxyEnv(proxyAddr != "")
|
|
}
|
|
}
|
|
opts := agentcore.Options{
|
|
Provider: c.prov,
|
|
SystemPrompt: system,
|
|
DynamicBoundary: boundary,
|
|
Tools: append(tools, deepSeekTools(ws)...),
|
|
DeferredTools: def.Deferred,
|
|
UnlockSet: def.Unlock,
|
|
PermissionMode: permission.ModeBypass,
|
|
EnableWebFetch: true, // 走记录代理留痕;载入代理 CA 验证 MITM 重签的 HTTPS 证书
|
|
WebFetchProxy: webFetchProxy,
|
|
WebFetchCACert: c.proxyCACert,
|
|
// WebSearchProxy 是独立出口代理(http/https/socks5),与记录流量的 MITM 代理无关;空则直连。
|
|
EnableWebSearch: ws.Enabled && ws.Backend != "deepseek",
|
|
WebSearchBackend: ws.Backend,
|
|
BraveSearchAPIKey: ws.BraveKey,
|
|
TavilySearchAPIKey: ws.TavilyKey,
|
|
WebSearchProxy: ws.Proxy,
|
|
BashEnv: bashEnv, // Bash 子命令默认走代理+信任 CA
|
|
WorkingDir: sessionWorkDir,
|
|
MaxDuration: maxDuration,
|
|
Compaction: compactionConfig(c.window),
|
|
Todos: actool.NewTodoStore(),
|
|
// large tool output spills to cmd-output/ under the session dir.
|
|
// 截断上限用 SDK 默认(tool.Capture 的 30000 字符)。
|
|
ToolOutputDir: filepath.Join(sessionWorkDir, "cmd-output"),
|
|
// 命中预算(步数)→ SDK 跑收尾:输出一句总结。Prompt 与收尾轮数按本 agent key 后台可编辑
|
|
// (自定义 agent 各自一份;留空/0 用通用默认:10 轮)。
|
|
Settlement: wrapupSettlement(agentKey, nil),
|
|
NonStreaming: c.nonStreaming(), // 该 profile 选非流式时走 Provider.Complete
|
|
MaxTokens: c.maxTokens(), // 0 = 不发上限,由服务端默认值决定
|
|
}
|
|
if c.guard != nil {
|
|
opts.Hooks = c.guard.Hooks()
|
|
}
|
|
if c.tx != nil { // persist raw human↔AI conversation; one accumulating file per thread
|
|
opts.Transcript = c.tx
|
|
opts.SessionID = sessionID
|
|
}
|
|
// 实验功能:开启后由 noa 接管上下文压缩(归档集中在 <workDir>/noa/<SessionID> 下,持久)。
|
|
enableNoa(&opts, c.noaEnabledFn, c.workDir, "chat-"+sessionID, noaWarn("chat-"+sessionID))
|
|
ctx = attachSideCapture(ctx, &opts)
|
|
s := newAgentSession(opts)
|
|
defer s.Close()
|
|
// reload prior conversation so the agent has context across turns (each Chat is
|
|
// a fresh session). First turn: no file yet → Resume loads nothing and proceeds.
|
|
if c.tx != nil {
|
|
_ = s.Resume(sessionID)
|
|
}
|
|
// re-unlock skill-gated MCPs from prior Skill() calls in the reloaded history so
|
|
// revealed tools stay callable across the fresh session.
|
|
seedUnlockFromHistory(s.Messages(), def.UnlockSkill)
|
|
text, _, err := captureRunSession(ctx, s, message, func(r db.Activity) {
|
|
if emit != nil {
|
|
r.Worker = agentKey
|
|
emit(r)
|
|
}
|
|
})
|
|
return text, err
|
|
}
|