terminal

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 13 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package terminal 管后台命令和交互终端的进程、输出、输入、时限和容量,不认识模型、JSON 和对话。

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTerminalNotFound       = errors.New("command is not available in this session and executor")
	ErrTerminalLimitReached   = errors.New("too many unfinished commands; stop one before starting another")
	ErrTerminalManagerStopped = errors.New("terminal manager has stopped")
)
View Source
var (
	ErrInvalidCursor    = errors.New("cursor is after the latest output")
	ErrInputUnavailable = errors.New("command was not started with interactive=true")
	ErrTerminalFinished = errors.New("command has finished")
)

Functions

This section is empty.

Types

type Terminal

type Terminal interface {
	// GetInfo 返回命令信息;结束前只有启动时的字段和 running。
	GetInfo() TerminalInfo
	// ReadOutput 返回 cursor 之后的输出:已有就立即返回,否则等新输出、命令结束或 wait 到期;只取消这次等待,不影响命令。
	ReadOutput(ctx context.Context, cursor uint64, wait time.Duration) (TerminalOutput, error)
	// WriteInput 把 chars 原样写进 PTY,返回实际写入的字节数;非 PTY 命令返回 ErrInputUnavailable,命令已结束返回 ErrTerminalFinished。
	// PTY 里的程序不读输入且 chars 超出 PTY 缓冲时,一直等到程序读走或写入失败;ctx 结束时立即返回 ctx 错误,
	// 已开始的写入留在后台,到程序读走或命令结束关闭终端时结束。
	WriteInput(ctx context.Context, chars string) (int, error)
	// Resize 修改 PTY 行列数,和当前尺寸相同时不处理;非 PTY 命令返回 ErrInputUnavailable,命令已结束返回 ErrTerminalFinished。
	// 和 WriteInput 按调用顺序依次执行。
	Resize(ctx context.Context, rows, cols uint16) (err error)
	// ClearOutput 丢掉保留的输出,只留 terminalClearSequence;命令已结束返回 ErrTerminalFinished。
	ClearOutput(ctx context.Context) (err error)
	// Stop 终止进程组并等到收尾完成,返回最终信息;已结束时直接返回。调用方取消等待不撤回已接受的停止。
	Stop(ctx context.Context) (TerminalInfo, error)
}

Terminal 是一次命令执行:持有进程、输出、PTY 输入、总时限和退出结果,可以跨 Turn 存活。

type TerminalInfo

type TerminalInfo struct {
	ID            string
	EnvironmentID string
	Command       string
	Directory     string
	// Interactive 是否用 PTY 启动命令?
	Interactive bool
	// Rows、Cols 是 PTY 行列数,非交互命令为零;GetInfo 在命令结束前返回初始值,TerminalOutput.Info 里是当前值。
	Rows, Cols uint16
	// StartedAt 是命令开始启动的时间。
	StartedAt time.Time
	Status    TerminalStatus
	// ExitCode 只有 exited 有;被信号终止时为 -1。
	ExitCode   *int
	StopReason TerminalStopReason
	// Err 是 failed 的原因,例如 SSH 断开后无法确认远端已停止。
	Err error
}

TerminalInfo 是一条命令的信息;结束前只有启动时的字段和 running 状态。

type TerminalManager

type TerminalManager interface {
	// Run 阻塞到 ctx 结束;之后不再接受新命令,停止并等待全部 Terminal 后返回。只能调用一次。
	Run(ctx context.Context) error
	// StartTerminal 启动一条命令;进程启动成功才返回 Terminal,失败时不返回编号。命令不随 ctx 取消。
	// request.OwnsWorkspace 为 true 时,容量已满、管理器已停止、启动失败也会关闭工作区。
	StartTerminal(ctx context.Context, request *TerminalRequest) (Terminal, error)
	// GetTerminal 按编号查 environmentID 上启动成功、还没淘汰的命令;否则返回 ErrTerminalNotFound。
	GetTerminal(environmentID, terminalID string) (Terminal, error)
	// ListTerminals 按启动先后返回 environmentID 上启动成功、还没淘汰的命令。
	ListTerminals(environmentID string) []Terminal
	// GetTerminalByID 按编号查启动成功、还没淘汰的命令;否则返回 ErrTerminalNotFound。
	GetTerminalByID(terminalID string) (result Terminal, err error)
	// ListAllTerminals 按启动先后返回启动成功、还没淘汰的全部命令。
	ListAllTerminals() (terminals []Terminal)
	// IsBusy 是否还有没结束的命令?正在启动的也算。
	IsBusy() bool
}

TerminalManager 是一组命令的集合:启动、查找、容量与淘汰;创建它的 Session 或 Runtime 结束时停止并等待全部命令。

func NewTerminalManager

func NewTerminalManager(opt *TerminalManagerOpt) (TerminalManager, error)

NewTerminalManager 校验并保存命令管理配置。

type TerminalManagerOpt

type TerminalManagerOpt struct {
	// MaxTerminals 是最多保留的命令数,所有环境合计;正在启动、启动失败和已结束未淘汰的都算。
	MaxTerminals int
	// MaxOutputBytes 是每条命令保留的输出末尾。
	MaxOutputBytes int
	// TerminalFinished 在命令结束后收到一次唤醒,通道已有未处理的唤醒时合并;为 nil 时不通知。
	TerminalFinished chan<- struct{}
}

TerminalManagerOpt 是命令容量和结束通知配置。

func NewTerminalManagerOpt

func NewTerminalManagerOpt() *TerminalManagerOpt

NewTerminalManagerOpt 创建命令容量和输出保留量的默认配置。

func (*TerminalManagerOpt) WithTerminalFinished added in v0.3.0

func (o *TerminalManagerOpt) WithTerminalFinished(terminalFinished chan<- struct{}) *TerminalManagerOpt

WithTerminalFinished 设置命令结束时的唤醒通道并返回本配置。

type TerminalOutput

type TerminalOutput struct {
	Info TerminalInfo
	// Text 从实际起点起最多 64 KiB,不切断 UTF-8 字符。
	Text       string
	NextCursor uint64
	// EndCursor 是读取这一刻已产生的输出总字节数,包括已被丢弃的部分。
	EndCursor uint64
	// HasMore 读取时还有保留的输出没返回?
	HasMore bool
	// Truncated 请求的 cursor 已被丢弃,Text 从保留的最早位置开始?
	Truncated bool
}

TerminalOutput 是一次读取的结果;Info 和输出出自同一时刻,状态已结束时输出也已收全。

type TerminalRequest

type TerminalRequest struct {
	EnvironmentID string
	Workspace     workspace.Workspace
	Command       string
	// Directory 是工作目录,绝对路径,由工具按工作区根目录算好。
	Directory string
	// Interactive 是否用 PTY 启动命令?是时 stdout 和 stderr 合并成一条输出流,之后可以写入输入。
	Interactive bool
	// Timeout 是从启动成功起的总运行时限,0 表示不限。
	Timeout time.Duration
	// Rows、Cols 是 PTY 初始行列数,为零时按 24×80;非交互命令忽略。
	Rows, Cols uint16
	// OwnsWorkspace 这条命令是否独占 Workspace?是时调用 StartTerminal 后工作区由本包关闭,调用方不再关闭。
	OwnsWorkspace bool
}

TerminalRequest 是启动一条命令要的全部事实;实际 Workspace 随请求传入,管理器不绑定环境。

func (*TerminalRequest) CloseWorkspace added in v0.3.0

func (r *TerminalRequest) CloseWorkspace(ctx context.Context)

CloseWorkspace 关闭命令独占的工作区;不独占时不处理,可重复调用。

type TerminalStatus

type TerminalStatus string

TerminalStatus 是命令的运行状态。

const (
	TerminalStatusRunning TerminalStatus = "running"
	TerminalStatusExited  TerminalStatus = "exited"
	TerminalStatusFailed  TerminalStatus = "failed"
)

type TerminalStopReason

type TerminalStopReason string

TerminalStopReason 是命令被停下的原因;命令自己结束时为空。

const (
	// TerminalStoppedByRequest 是调用方通过 Terminal.Stop 停下的,例如 Agent 的 stop_command 或用户关闭终端。
	TerminalStoppedByRequest TerminalStopReason = "stopped"
	TerminalStoppedByTimeout TerminalStopReason = "timed_out"
	// TerminalStoppedByManager 是 TerminalManager 停止(Run 的 ctx 结束)时停下的;值 session_closed 会进工具结果和已保存的记录,保持不变。
	TerminalStoppedByManager TerminalStopReason = "session_closed"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL