package tool import ( "context" "encoding/json" "fmt" "regexp" "time" "github.com/Autumn-27/norma/permission" ) // The interactive-shell tool family (opt-in; NOT in DefaultTools). ShellSessionTools // returns all five. They require a task Manager on the ToolContext (tc.Tasks) — that is // where the PTY sessions live and get cleaned up. See docs/交互式shell设计.md. // ShellSessionTools returns the five interactive-shell tools. Callers add them to // Options.Tools only when interactive shell is enabled for the agent. func ShellSessionTools() []CoreTool { return []CoreTool{NewShellOpen(), NewShellSend(), NewShellRead(), NewShellClose(), NewShellList()} } func sessTool(name, desc string, schema map[string]any, readOnly bool, run func(context.Context, json.RawMessage, *ToolContext) (Result, error)) CoreTool { return Build(Spec{ Name: name, Description: desc, Schema: schema, ReadOnly: func(json.RawMessage) bool { return readOnly }, Concurrent: func(json.RawMessage) bool { return false }, Permissions: func(context.Context, json.RawMessage, permission.Context) permission.Decision { return permission.Allowed() }, Run: run, }) } func obj(props map[string]any, required ...string) map[string]any { m := map[string]any{"type": "object", "properties": props} if len(required) > 0 { m["required"] = required } return m } func strProp(d string) map[string]any { return map[string]any{"type": "string", "description": d} } func boolProp(d string) map[string]any { return map[string]any{"type": "boolean", "description": d} } func intProp(d string) map[string]any { return map[string]any{"type": "integer", "description": d} } func arrStr(d string) map[string]any { return map[string]any{"type": "array", "items": map[string]any{"type": "string"}, "description": d} } func jsonResult(v any) (Result, error) { b, err := json.Marshal(v) if err != nil { return Errorf("marshal: " + err.Error()), nil } return Text(string(b)), nil } func sessManager(tc *ToolContext) (*Manager, *Result) { if InteractiveShellDisabled() { r := Errorf("交互式 shell 已被全局停用(AGENT_CORE_DISABLE_INTERACTIVE_SHELL)") return nil, &r } if tc == nil || tc.Tasks == nil { r := Errorf("交互式 shell 需要后台任务管理器(未启用)") return nil, &r } return tc.Tasks, nil } func ms(v int, def int) time.Duration { if v <= 0 { v = def } return time.Duration(v) * time.Millisecond } // NewShellOpen 开一个交互会话。 func NewShellOpen() CoreTool { desc := "开一个持久 PTY 交互会话运行【交互式/需真 TTY 的程序】(msfconsole/ssh 交互/mysql、python 等 REPL/密码提示/nc 会话),返回 session_id。**一次性、非交互命令用 Bash**。mode: interactive=阻塞到首个提示符/静默再返回启动输出; hands-free(默认)=立即返回, 之后用 shell_read 轮询; dispatch=立即返回, 完成时发通知; monitor=立即返回, 后台盯 watch 关键词命中即发通知(会话不结束, 仅本次运行内存活)。可设 prompt_regex(如 'msf6? >\\s*$')精确判定完成。用完务必 shell_close。" schema := obj(map[string]any{ "command": strProp("要运行的交互程序,如 'msfconsole -q' / 'ssh user@host' / 'python3'"), "mode": strProp("interactive | hands-free(默认) | dispatch | monitor"), "watch": arrStr("monitor 模式必填:要盯的关键词/正则数组,输出里命中任一即发通知(每个只报一次)"), "prompt_regex": strProp("可选:命令完成判定的提示符正则(匹配输出末尾)"), "rows": intProp("可选:PTY 行数(默认 24)"), "cols": intProp("可选:PTY 列数(默认 80)"), "quiet_ms": intProp("可选:静默多少毫秒算完成(默认 8000)"), "startup_grace_ms": intProp("可选:启动宽限毫秒,此期间不触发静默判定(默认 15000)"), }, "command") return sessTool("shell_open", desc, schema, false, func(ctx context.Context, in json.RawMessage, tc *ToolContext) (Result, error) { m, errR := sessManager(tc) if errR != nil { return *errR, nil } var a struct { Command string `json:"command"` Mode string `json:"mode"` PromptRegex string `json:"prompt_regex"` Watch []string `json:"watch"` Rows int `json:"rows"` Cols int `json:"cols"` QuietMs int `json:"quiet_ms"` StartupGraceMs int `json:"startup_grace_ms"` } _ = json.Unmarshal(in, &a) if a.Command == "" { return Errorf("command 必填"), nil } mode := a.Mode if mode == "" { mode = "hands-free" } switch mode { case "hands-free", "interactive", "dispatch", "monitor": default: return Errorf("mode 需为 interactive | hands-free | dispatch | monitor"), nil } var watch []*regexp.Regexp if mode == "monitor" { if len(a.Watch) == 0 { return Errorf("monitor 模式必须给 watch(要盯的关键词/正则)"), nil } for _, w := range a.Watch { re, err := regexp.Compile(w) if err != nil { return Errorf("watch 正则无效 " + w + ": " + err.Error()), nil } watch = append(watch, re) } } var pr *regexp.Regexp if a.PromptRegex != "" { re, err := regexp.Compile(a.PromptRegex) if err != nil { return Errorf("prompt_regex 无效: " + err.Error()), nil } pr = re } workDir := "" var env []string if tc != nil { workDir, env = tc.WorkingDir, tc.Env } s, err := m.openSession(a.Command, workDir, env, a.Rows, a.Cols, mode, pr) if err != nil { return Errorf("开启会话失败: " + err.Error()), nil } quiet, grace := ms(a.QuietMs, sessDefQuietMs), ms(a.StartupGraceMs, sessDefGraceMs) switch mode { case "interactive": by := s.waitComplete(ctx, quiet, grace, ms(0, sessWaitCapMs), pr, "") out, cur, _ := s.readSince(0, 0, sessDefReadByte*4, false) return jsonResult(map[string]any{"session_id": s.id, "state": "running", "done_by": by, "output": out, "cursor": cur, "running": s.running()}) case "dispatch": go m.dispatchWaiter(s, quiet, grace, ms(0, sessWaitCapMs)) return jsonResult(map[string]any{"session_id": s.id, "state": "running", "dispatched": true}) case "monitor": go m.monitorWaiter(s, watch) return jsonResult(map[string]any{"session_id": s.id, "state": "running", "monitoring": len(watch)}) default: // hands-free return jsonResult(map[string]any{"session_id": s.id, "state": "running"}) } }) } // NewShellSend 向会话发送输入/按键。 func NewShellSend() CoreTool { desc := "向一个交互会话发送输入:text/submit/keys/hex/paste 可组合,按出现顺序发送(各参数含义见字段)。wait=true(默认)等命令完成并带回增量输出。" schema := obj(map[string]any{ "session_id": strProp("会话 id(shell_open 返回)"), "text": strProp("要输入的文本(不自动回车)"), "submit": boolProp("是否在文本后追加回车执行"), "keys": arrStr("具名键: ctrl+c/ctrl+d/tab/enter/esc/up/down/left/right/..."), "hex": arrStr("原始字节的十六进制, 如 ['0x1b','0x5b','0x41']"), "paste": strProp("多行粘贴(bracketed paste, 不触发逐行执行)"), "wait": boolProp("是否同步等到命令完成(默认 true)"), "prompt_regex": strProp("可选:覆盖会话默认的完成判定提示符"), "quiet_ms": intProp("可选:静默判定阈值毫秒(默认 8000)"), "timeout_ms": intProp("可选:wait 的硬超时毫秒(默认 120000)"), }, "session_id") return sessTool("shell_send", desc, schema, false, func(ctx context.Context, in json.RawMessage, tc *ToolContext) (Result, error) { m, errR := sessManager(tc) if errR != nil { return *errR, nil } var a struct { SessionID string `json:"session_id"` Text string `json:"text"` Paste string `json:"paste"` PromptRegex string `json:"prompt_regex"` Submit bool `json:"submit"` Keys []string `json:"keys"` Hex []string `json:"hex"` Wait *bool `json:"wait"` QuietMs int `json:"quiet_ms"` TimeoutMs int `json:"timeout_ms"` } _ = json.Unmarshal(in, &a) s, ok := m.session(a.SessionID) if !ok { return Errorf("会话不存在: " + a.SessionID), nil } if !s.running() { return Errorf("会话已结束(exit " + fmtExit(s.exit()) + "),用 shell_read 读取尾部输出或 shell_close"), nil } before := s.total // build & write input in order: text, submit(\r), keys, hex, paste var payload []byte if a.Text != "" { payload = append(payload, a.Text...) } if a.Submit { payload = append(payload, '\r') } for _, k := range a.Keys { if b, ok := keyToBytes(k); ok { payload = append(payload, b...) } else { return Errorf("未知按键: " + k), nil } } if len(a.Hex) > 0 { payload = append(payload, hexToBytes(a.Hex)...) } if a.Paste != "" { payload = append(payload, "\x1b[200~"...) payload = append(payload, a.Paste...) payload = append(payload, "\x1b[201~"...) } if len(payload) == 0 { return Errorf("没有可发送的输入(text/submit/keys/hex/paste 至少给一个)"), nil } if _, err := s.ptmx.Write(payload); err != nil { return Errorf("写入失败: " + err.Error()), nil } wait := a.Wait == nil || *a.Wait if !wait { return jsonResult(map[string]any{"session_id": s.id, "sent": len(payload), "running": s.running()}) } pr := s.prompt if a.PromptRegex != "" { if re, err := regexp.Compile(a.PromptRegex); err == nil { pr = re } } by := s.waitComplete(ctx, ms(a.QuietMs, sessDefQuietMs), 0, ms(a.TimeoutMs, sessWaitCapMs), pr, "") out, cur, omitted := s.readSince(before, 0, sessDefReadByte, false) return jsonResult(map[string]any{"session_id": s.id, "output": out, "done_by": by, "cursor": cur, "running": s.running(), "omitted": omitted}) }) } // NewShellRead 读输出:增量流 或 整屏快照。 func NewShellRead() CoreTool { desc := "读一个交互会话的输出(不发输入)。view=stream(默认)增量原始流(流式首选,续读/可 drain 取全量); view=screen 整屏 vt 快照(适合 vim/htop 等全屏程序)。" schema := obj(map[string]any{ "session_id": strProp("会话 id"), "view": strProp("stream(默认) | screen"), "since_cursor": intProp("stream: 从该游标之后读; 不传则用会话内部游标续读"), "max_lines": intProp("stream: 最多返回行数(默认 200)"), "max_bytes": intProp("stream: 最多返回字节(默认 8192)"), "drain": boolProp("stream: true=返回自上次以来全部原始流(忽略上限)"), }, "session_id") // per-session read cursor kept here (tool-side) so unspecified reads continue. return sessTool("shell_read", desc, schema, true, func(_ context.Context, in json.RawMessage, tc *ToolContext) (Result, error) { m, errR := sessManager(tc) if errR != nil { return *errR, nil } var a struct { SessionID string `json:"session_id"` View string `json:"view"` SinceCursor *int64 `json:"since_cursor"` MaxLines int `json:"max_lines"` MaxBytes int `json:"max_bytes"` Drain bool `json:"drain"` } _ = json.Unmarshal(in, &a) s, ok := m.session(a.SessionID) if !ok { return Errorf("会话不存在: " + a.SessionID), nil } if a.View == "screen" { grid, rows, cols, cx, cy := s.screen() return jsonResult(map[string]any{"view": "screen", "screen": grid, "rows": rows, "cols": cols, "cursor": map[string]int{"row": cy, "col": cx}, "running": s.running()}) } var from int64 if a.SinceCursor != nil { from = *a.SinceCursor } else { from = s.readCursor() } maxLines, maxBytes := a.MaxLines, a.MaxBytes if maxLines == 0 { maxLines = sessDefReadLine } if maxBytes == 0 { maxBytes = sessDefReadByte } out, cur, omitted := s.readSince(from, maxLines, maxBytes, a.Drain) s.setReadCursor(cur) res := map[string]any{"view": "stream", "output": out, "cursor": cur, "running": s.running(), "omitted": omitted} if ec := s.exit(); ec != nil { res["exit_code"] = *ec } return jsonResult(res) }) } // NewShellClose 关闭会话。 func NewShellClose() CoreTool { schema := obj(map[string]any{"session_id": strProp("会话 id")}, "session_id") return sessTool("shell_close", "关闭一个交互会话(杀掉进程、释放 PTY)。", schema, false, func(_ context.Context, in json.RawMessage, tc *ToolContext) (Result, error) { m, errR := sessManager(tc) if errR != nil { return *errR, nil } var a struct { SessionID string `json:"session_id"` } _ = json.Unmarshal(in, &a) s, ok := m.session(a.SessionID) if !ok { return Errorf("会话不存在: " + a.SessionID), nil } ec := s.exit() s.kill() m.sessMu.Lock() delete(m.sessions, a.SessionID) m.sessMu.Unlock() return jsonResult(map[string]any{"closed": a.SessionID, "exit_code": exitOrNil(ec)}) }) } // NewShellList 列出会话。 func NewShellList() CoreTool { return sessTool("shell_list", "列出当前所有交互会话及状态。", obj(map[string]any{}), true, func(_ context.Context, _ json.RawMessage, tc *ToolContext) (Result, error) { m, errR := sessManager(tc) if errR != nil { return *errR, nil } m.reapSessions() m.sessMu.Lock() defer m.sessMu.Unlock() out := make([]map[string]any, 0, len(m.sessions)) for _, s := range m.sessions { s.mu.Lock() out = append(out, map[string]any{"session_id": s.id, "command": s.command, "state": s.state, "mode": s.mode, "idle_ms": time.Since(s.lastOut).Milliseconds()}) s.mu.Unlock() } return jsonResult(map[string]any{"sessions": out}) }) } // --- small helpers --- func fmtExit(ec *int) string { if ec == nil { return "?" } return fmt.Sprintf("%d", *ec) } func exitOrNil(ec *int) any { if ec == nil { return nil } return *ec }