Documentation
¶
Index ¶
Constants ¶
const ( ModeHost = "host" ModeContainer = "container" )
执行位置常量:避免 hostshell 依赖 config 包,取值与 config.BashExecutionMode* 保持一致。 ModeHost 是默认值:Mode 为空串或 ModeHost 都走宿主机 shell。
Variables ¶
This section is empty.
Functions ¶
func CloseAllPersistentShells ¶
func CloseAllPersistentShells()
CloseAllPersistentShells 关闭所有会话的 shell(进程退出时兜底)。
func CloseContainerShell ¶
func CloseContainerShell(agentSessionID int64)
CloseContainerShell 关闭 agentSessionID 对应的容器 shell(会话结束后调用)。
func ClosePersistentShell ¶
func ClosePersistentShell(sessionID int64)
ClosePersistentShell 关闭 sessionID 对应的 shell(会话结束后调用)。
TODO: 子 Agent 复用父 Agent 的 SessionID(见 native.NewAgentTool 的 ParentSessionID), 因此父请求结束时直接关闭会打断子 Agent 正在执行的命令。待实现:按会话统计活跃执行数, 归零时才真正 kill(OnStart +1 / OnFinish -1),子 Agent 复用同会话时计数不会归零。
func CloseShell ¶
func CloseShell(sessionID int64)
CloseShell 关闭 sessionID 对应的所有终端(宿主机 shell 与容器 shell), 供 Agent 请求结束时统一回收;可重复调用。
这是包内唯一的终端回收入口:Shell 接口故意不暴露 Close, 因为终端按会话(而非按实例)登记,回收必须同时命中两类终端。
Types ¶
type ContainerShell ¶
type ContainerShell struct {
// contains filtered or unexported fields
}
ContainerShell 是一个 Agent 会话对应的容器持久 shell。
语义与 PersistentShell 完全一致:一次 Agent 请求内多次调用复用同一个容器 PTY, 请求结束时由调用方 Close 掉(见 providers/custom.go 的 OnFinish)。
func GetContainerShell ¶
func GetContainerShell(svc ContainerShellService, agentSessionID, userID, projectID, templateID int64) *ContainerShell
GetContainerShell 返回 agentSessionID 对应的容器 shell:同一会话内复用(容器与 PTY 保留), 会话结束后调用方必须调用 CloseContainerShell 释放容器。
svc 为 nil 或 templateID <= 0 时返回 nil(未启用容器执行)。
type ContainerShellService ¶
type ContainerShellService interface {
// Ensure 返回 terminalID 对应的终端会话 ID;不存在时按 (userID, projectID, templateID) 创建。
Ensure(ctx context.Context, userID, projectID, templateID, agentSessionID int64) (int64, error)
// Exec 在 terminalID 对应的终端里执行命令(等待结束)。
Exec(ctx context.Context, terminalID int64, command string, timeoutSeconds int) (runtimecontext.ContainerTerminalExecResult, error)
// Close 关闭 terminalID 对应的终端会话(删除容器)。
Close(ctx context.Context, terminalID int64)
}
ContainerShellService 是 manager 提供的「容器终端」最小能力集。
与 hostshell 里的持久 shell 一一对应:
- Ensure 相当于 GetPersistentShell(按会话定位 / 创建容器终端,保留 cd / export);
- Exec 相当于 PersistentShell.Exec(把命令写入同一个 PTY 并等待结果);
- Close 相当于 ClosePersistentShell(结束容器并删除会话记录)。
type PersistentShell ¶
type PersistentShell struct {
// contains filtered or unexported fields
}
func GetPersistentShell ¶
func GetPersistentShell(sessionID int64, workingDir string) *PersistentShell
GetPersistentShell 返回 sessionID 对应的持久 shell:同一会话内复用(cwd / 环境变量保留), 会话不同则各自新建,可并发执行。会话结束后调用方必须调用 ClosePersistentShell 释放进程。
func (*PersistentShell) Close ¶
func (s *PersistentShell) Close(_ context.Context)
Close 实现 Shell:ctx 仅用于接口统一,宿主机进程结束不依赖它。
type Shell ¶
type Shell interface {
// Exec 在终端里执行命令并等待结束。
//
// 返回值与 bash 工具对齐的四元组:stdout、stderr、exitCode、interrupted。
Exec(ctx context.Context, command string, timeoutMs int) (stdout, stderr string, exitCode int, interrupted bool, err error)
}
Shell 是 Agent 一次 bash 调用的「命令执行终端」统一抽象。
两个实现:
- PersistentShell(shell_unix.go / shell_windows.go):宿主机常驻 shell/PowerShell;
- ContainerShell(container_shell.go):用户容器模板里的持久 PTY。
二者语义完全一致:同一个 Agent 会话内的多次调用复用同一个终端 (cwd / 环境变量 / 已安装的包在调用之间保留),会话结束时由调用方通过包级 CloseShell(sessionID) 统一回收。 工具层只依赖本接口,通过 AcquireShell 按配置拿到具体实现,不必感知执行位置。
func AcquireShell ¶
func AcquireShell(options ShellOptions) Shell
AcquireShell 按 options 返回统一 Shell 接口的实现。
host 模式返回宿主机 *PersistentShell;container 模式返回 *ContainerShell。 未启用(宿主机 shell 创建失败、容器服务缺失、模板 / 会话 / 项目信息不足)时返回 nil, 调用方据此给出可读的错误。
type ShellOptions ¶
type ShellOptions struct {
// Mode 是执行位置:config.BashExecutionModeHost 或 config.BashExecutionModeContainer。
// 传空串时按 host 处理。
Mode string
// SessionID 是 Agent 会话 ID:同会话复用同一个终端,必须 > 0。
SessionID int64
// WorkingDir 是宿主机终端的初始工作目录(仅 host 模式使用)。
WorkingDir string
// Container 模式下需要的定位与能力:
// - 需要 SessionID / UserID / ProjectID 定位用户激活项目下的容器终端;
// - TemplateID 指定容器模板;ContainerShell 提供容器终端服务能力。
UserID int64
ProjectID int64
TemplateID int64
Container ContainerShellService
}
ShellOptions 是 AcquireShell 的入参,描述「为哪个会话、在什么位置」取终端。