Help us shape the future of Go! Take the 2026 Go Developer Survey

session

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrSessionBusy = errors.New("session is busy")

ErrSessionBusy 是 Session 忙时拒绝请求的错误:idle_only 输入遇到正在执行的 Turn,或手动压缩遇到正在执行的 Turn、排队的用户输入。

Functions

This section is empty.

Types

type PendingInput

type PendingInput struct {
	ItemID     defs.ItemID `json:"item_id"`
	Preview    string      `json:"preview"`
	ImageCount int         `json:"image_count"`
	IsSteer    bool        `json:"is_steer"`
}

PendingInput 是尚未交给 Turn 的一条用户输入的轻量展示内容。

type Session

type Session interface {
	// GetID 返回 Session ID。
	GetID() defs.SessionID
	// Run 修复最近的 Turn 后串行处理收件箱;停止时取消活动 Turn、丢弃排队输入,等 Turn 和后台命令结束后返回。
	// events 不能为 nil,调用方要一直读到它关闭;events 关闭时,其他方法已经按 Session 已停止处理。
	Run(ctx context.Context, events chan<- SessionEvent) error
	// HandleInput 接收输入并返回 ItemID,同一 InputID 在本次运行内只接收一次;哪个 Turn 处理了它,看 SessionTurnFinished.InputItemIDs。
	// 输入无效、Session 已开始停止、所选模型不在目录里、选中的 Skill 读不出来或 idle_only 遇到活动 Turn 时返回错误,这条输入不排队;
	// 重新选择模型后可以再提交。
	HandleInput(ctx context.Context, input *SessionInput) (defs.ItemID, error)
	// GetStatus 返回执行状态和最近 Turn,停止后返回最终状态;查询可能短暂落后于已发出的 Turn 结束事件。
	GetStatus(ctx context.Context) (SessionStatus, error)
	// IsTerminalBusy 本 Session 是否还有未结束或正在启动的后台命令?只查命令记录,与是否在执行 Turn 无关。
	IsTerminalBusy() bool
	// InterruptTurn 请求取消活动 Turn 并返回它的 ID,不等取消完成;没有活动 Turn 时返回空。
	// 根 Session 被中断后,Agent 消息继续排队但不启动 Turn,收到下一条用户输入后恢复。
	InterruptTurn(ctx context.Context) (defs.TurnID, error)
	// SteerInput 将普通排队用户输入完整移入指定轮的未读补充。
	SteerInput(ctx context.Context, itemID defs.ItemID, turnID defs.TurnID) (err error)
	// InterruptTurnWithInput 将选中用户输入与旧轮未读补充同批置顶,并取消指定轮。
	InterruptTurnWithInput(ctx context.Context, turnID defs.TurnID, itemID defs.ItemID) (err error)
	// WithdrawInput 原子取走待处理的完整用户输入组。
	WithdrawInput(ctx context.Context, itemID defs.ItemID) (items []*defs.Item, err error)
	// WaitSteer 等活动 Turn 的未读补充输入,返回副本,不取走;已有输入时立即返回,没有活动 Turn、Turn 结束或 Session 已停止时返回空、不报错。
	// 调用方先取消时返回 ctx.Err()。
	WaitSteer(ctx context.Context) ([]*defs.Item, error)
	// CompactContext 空闲时启动只压缩的 Turn 并返回它的 ID,不等压缩完成;有活动 Turn 或排队的用户输入时返回 ErrSessionBusy,
	// 排队的 Agent 消息不算忙;已开始停止或所选模型不在目录里时返回错误。
	CompactContext(ctx context.Context) (defs.TurnID, error)
	// SelectModel 接收 Conversation 已校验并保存的模型选择,从下个 Turn 起使用,不影响正在执行的 Turn;Session 已停止时返回 nil。
	SelectModel(ctx context.Context, model defs.ModelSelection) error
}

Session 接收输入、串行执行 Turn,管理本 Session 的补充输入和后台命令。

func NewSession

func NewSession(opt *SessionOpt) (Session, error)

NewSession 校验配置,创建本 Session 的后台命令管理和工具表,返回还没运行的 Session。

type SessionAgentEvent

type SessionAgentEvent struct {
	TurnID defs.TurnID
	Event  *agent.AgentEvent
}

SessionAgentEvent 由 Turn 执行任务在收到 TurnAgentEvent 后转发,携带对应的 Agent 事件。

type SessionEvent

type SessionEvent interface {
	// contains filtered or unexported methods
}

SessionEvent 是 Session.Run 的 events 里的事件,由各 Turn 执行任务发出。

type SessionHookRunEvent

type SessionHookRunEvent struct {
	TurnID defs.TurnID
	Run    *defs.HookRun
}

SessionHookRunEvent 由 Turn 执行任务转发 Turn 自己触发的 hook(SessionStart、SubagentStart、UserPromptSubmit)的运行记录: 每次运行开始(running)和结束(最终状态)各发一次,同一次运行的 ID 不变。Agent 触发的 hook 运行经 SessionAgentEvent 到达。

type SessionInput

type SessionInput struct {
	Mode SessionInputMode
	// Item 是输入消息,ID 由调用方生成,还没分配 TurnID 和 ModelCallID。
	Item *defs.Item
	// InputID 可选:本次运行期内重复的 InputID 只接收一次,返回首次接收的 ItemID。
	InputID defs.InputID
}

SessionInput 是交给 Session 的一次输入。

type SessionInputMode

type SessionInputMode string

SessionInputMode 是一条输入的提交方式。Agent 发来的消息不插队,没挂上活动 Turn 的都排进 Agent 消息队列。

const (
	// SessionInputModeNormal 排队等新 Turn,空闲时立刻开始。
	SessionInputModeNormal SessionInputMode = "normal"
	// SessionInputModeIdleOnly 只在没有活动 Turn 时接收并立刻开始,忙时返回 ErrSessionBusy,不排队。
	SessionInputModeIdleOnly SessionInputMode = "idle_only"
	// SessionInputModeSteer 挂到还在接收补充输入的活动 Turn 上;没有这样的 Turn 时按 normal 排队。
	SessionInputModeSteer SessionInputMode = "steer"
	// SessionInputModeInterrupt 插队:取消活动 Turn,这条排到用户队列最前;空闲时和 normal 一样立刻开始。
	SessionInputModeInterrupt SessionInputMode = "interrupt"
)

type SessionItemsSaved

type SessionItemsSaved struct {
	TurnID defs.TurnID
	Items  []dao.LoggedItem
}

SessionItemsSaved 由 Turn 执行任务在 Turn 写入一批 Item 后转发;Items 按序号递增, Agent 输出 Item 的保存事件总在它的终态 SessionAgentEvent 之后。

type SessionOpt

type SessionOpt struct {
	// Session 是持久化的 Session 记录。
	Session  defs.Session
	Query    dao.Query
	Mutation dao.Mutation
	Client   llmkit.Client
	// Agent 是本 Session 每个 Turn 使用的 Agent 定义。
	Agent *agent.AgentDefinition
	// Models 是只读的模型目录,由 Conversation 建立索引后共享给全部 Session。
	Models map[defs.ModelRef]*defs.ModelDefinition
	// Environments 是本对话的执行环境实例,第一个为默认;由 Conversation 持有,Session 只借用。
	Environments []executor.ExecutionEnvironment
	// AgentTools 是 Conversation 为本 Session 建的协作工具,在 Agent 侧执行。
	AgentTools []executor.Tool
	// ClientMCPServers 提供 Conversation 持有的客户端 MCP 工具,nil 表示没有。
	ClientMCPServers executor.ClientMCPServers
	// Skills 是本 Agent 可用的 Skill,接收用户输入时按消息里的斜杠指令读取正文。
	Skills []*skill.Skill
	// Hooks 交给每个 Turn 触发 SessionStart、SubagentStart、UserPromptSubmit 与 Stop 等事件;nil 表示没有 hook。
	Hooks hook.Hooks
	// HookStartSource 是本次启动后第一个非压缩 Turn 触发 SessionStart 的来源:startup 或 resume;为空时不触发
	// (重开的子 Session)。子 Session 的 Turn 按它触发 SubagentStart。
	HookStartSource string
}

SessionOpt 是创建 Session 用的配置:Session 记录、DAO、模型客户端、Agent 定义,以及 Conversation 交来的执行环境、工具、Skill 和 hook。

func NewSessionOpt

func NewSessionOpt(
	session defs.Session,
	query dao.Query,
	mutation dao.Mutation,
	client llmkit.Client,
	agentDefinition *agent.AgentDefinition,
) *SessionOpt

NewSessionOpt 用 Session 记录、DAO、模型客户端和 Agent 定义创建 SessionOpt,其余字段由调用方填写。

type SessionPendingInputs

type SessionPendingInputs struct {
	SteerTurnID defs.TurnID    `json:"steer_turn_id,omitempty"`
	Inputs      []PendingInput `json:"inputs"`
}

SessionPendingInputs 是待处理用户输入及仍接收补充输入的轮。

type SessionPendingInputsUpdated

type SessionPendingInputsUpdated struct {
	PendingInputs SessionPendingInputs
}

SessionPendingInputsUpdated 由 Session 在输入进入、移动、取走或补充接收状态变化后发出。

type SessionState

type SessionState string

SessionState 是 Session 在查询时所处的阶段。

const (
	// SessionStateIdle 没有活动 Turn。
	SessionStateIdle SessionState = "idle"
	// SessionStateRunning 有活动 Turn。
	SessionStateRunning SessionState = "running"
	// SessionStateStopping 已开始停止,在等活动 Turn 结束。
	SessionStateStopping SessionState = "stopping"
	// SessionStateStopped Session.Run 已返回,或 Session 没在运行。
	SessionStateStopped SessionState = "stopped"
)

type SessionStatus

type SessionStatus struct {
	State SessionState `json:"state"`
	// ActiveTurnID 是活动 Turn,没有时为空。
	ActiveTurnID defs.TurnID `json:"active_turn_id,omitempty"`
	// PendingInputCount 是排队等新 Turn 的输入条数,用户输入和 Agent 消息都算。
	PendingInputCount int `json:"pending_input_count"`
	// LastTurnID 是最近一个 Turn。
	LastTurnID defs.TurnID `json:"last_turn_id,omitempty"`
	// LastTurnStatus 是最近一个 Turn 保存的终态,终态没保存时为空。
	LastTurnStatus defs.TurnStatus `json:"last_turn_status,omitempty"`
}

SessionStatus 是查询时 Session 的执行状态和最近 Turn。

type SessionTurnFinished

type SessionTurnFinished struct {
	TurnID defs.TurnID
	// InputItemIDs 是本轮读入的全部输入,包括补充输入。
	InputItemIDs []defs.ItemID
	// Status 是本轮的终态,只在终态已保存时填写。
	Status defs.TurnStatus
	// Outputs 是本轮的输出 Item。
	Outputs []*defs.Item
	// FinalModelCallID 是最后一次模型调用的 ID,只在 completed 时填写。
	FinalModelCallID defs.ModelCallID
	// Err 是 Turn.Run 返回的错误。
	Err error
}

SessionTurnFinished 由 Turn 执行任务在 Turn.Run 返回(终态已尽力写库)后发出;正常结束、失败、被中断都会发。

type SessionTurnStarted

type SessionTurnStarted struct {
	TurnID defs.TurnID
	// Model 是本 Turn 使用的模型和推理强度。
	Model defs.ModelSelection
}

SessionTurnStarted 由 Turn 执行任务在调用 Turn.Run 之前发出;同一 Turn 的其他事件都在它之后。

Jump to

Keyboard shortcuts

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