host

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildStartPrompt

func BuildStartPrompt(prompt string) string

BuildStartPrompt 将用户需求包装为 Coordinator 的启动 prompt。

func ReplayDeltaText

func ReplayDeltaText(item domain.RuntimeQueueItem) string

ReplayDeltaText 从运行时队列项中提取可回放的流式文本。

Types

type AgentContextSnapshot

type AgentContextSnapshot struct {
	Tokens          int
	ContextWindow   int
	Percent         float64
	Scope           string
	Strategy        string
	ActiveMessages  int
	SummaryMessages int
	CompactedCount  int
	KeptCount       int
}

AgentContextSnapshot 是 Agent 上下文使用情况。

type AgentSnapshot

type AgentSnapshot struct {
	Name             string
	State            string
	TaskID           string
	TaskKind         string
	Summary          string
	Tool             string
	Turn             int
	Context          AgentContextSnapshot
	RecentProjection AgentContextSnapshot
	UpdatedAt        time.Time
}

AgentSnapshot 是 Agent 状态的展示投影。

type CoCreateMessage

type CoCreateMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

CoCreateMessage 是共创对话的消息。

type CoCreateReply

type CoCreateReply struct {
	Message string
	Prompt  string
	Ready   bool
}

CoCreateReply 是共创对话的 LLM 回复。

type Event

type Event struct {
	ID         string    // 同一次调用的开始/结束共用;非调用事件为空
	Time       time.Time // 首次发出时间(开始时刻)
	FinishedAt time.Time // 零值 = 进行中;非零 = 已完成
	Failed     bool      // 已完成但失败(仅完成态有意义)
	Category   string    // DISPATCH / TOOL / SYSTEM / REVIEW / CHECK / ERROR / CONTEXT
	Agent      string    // 产生事件的 agent
	Summary    string
	Level      string        // info / warn / error / success
	Depth      int           // 0 = coordinator 层, 1 = sub-agent 层
	Duration   time.Duration // 完成时的执行耗时
}

Event 是 TUI 消费的结构化事件。

对于 TOOL / DISPATCH 两类调用事件,同一次调用的开始与结束共用一个 ID: 开始时先发 FinishedAt 为零值的事件(TUI 渲染为"进行中"样式); 结束时再发一条同 ID 的事件,填入 FinishedAt + Duration(+ Failed), TUI 按 ID 定位原行原地更新,避免"开始一行、完成又一行"的冗余。

SYSTEM / ERROR / CONTEXT 等非调用类事件 ID 为空,每条独立追加。

func (Event) Running added in v0.1.2

func (e Event) Running() bool

Running 返回事件是否处于进行中。 仅调用类事件(有 ID 的 TOOL / DISPATCH)可能进行中;其它类型总是返回 false。

type Host

type Host struct {
	// contains filtered or unexported fields
}

Host 是运行时薄外壳。 职责:启动/恢复/干预注入/事件投影/模型管理。 不做任何调度决策,不做空闲续跑。

func New

func New(cfg bootstrap.Config, bundle assets.Bundle) (*Host, error)

New 创建 Host。

func (*Host) Abort

func (h *Host) Abort() bool

Abort 暂停当前 coordinator。

func (*Host) AskUser

func (h *Host) AskUser() *tools.AskUserTool

func (*Host) Close

func (h *Host) Close()

Close 终止 coordinator 并关闭事件通道。

func (*Host) CoCreateStream

func (h *Host) CoCreateStream(ctx context.Context, history []CoCreateMessage, onReply func(string)) (CoCreateReply, error)

func (*Host) ConfiguredModels

func (h *Host) ConfiguredModels(provider string) []string

func (*Host) ConfiguredProviders

func (h *Host) ConfiguredProviders() []string

func (*Host) Continue

func (h *Host) Continue(text string) error

Continue 用指定 prompt 继续。停机后用户在输入框输入时调用。

func (*Host) CurrentModelSelection

func (h *Host) CurrentModelSelection(role string) (string, string, bool)

func (*Host) Dir

func (h *Host) Dir() string

func (*Host) Done

func (h *Host) Done() <-chan struct{}

func (*Host) Events

func (h *Host) Events() <-chan Event

func (*Host) ImportFrom added in v0.3.0

func (h *Host) ImportFrom(ctx context.Context, opts imp.Options) (<-chan imp.Event, error)

ImportFrom 启动一次外部小说反推导入:切分 → 反推 foundation → 逐章分析落盘。 与 Coordinator 互斥;导入完成后调用方可立即 Resume() 续写。 返回的事件通道由 imp.Run 关闭,调用方负责消费(满则丢弃以防阻塞分析协程)。

func (*Host) ReplayQueue

func (h *Host) ReplayQueue(afterSeq int64) ([]domain.RuntimeQueueItem, error)

func (*Host) Resume

func (h *Host) Resume() (string, error)

Resume 恢复模式:从 checkpoint + progress 生成 resume prompt 并启动。

func (*Host) Snapshot

func (h *Host) Snapshot() UISnapshot

func (*Host) Start

func (h *Host) Start(prompt string) error

Start 新建模式:初始化进度并启动 coordinator 长循环。

func (*Host) StartPrepared

func (h *Host) StartPrepared(promptText string) error

StartPrepared 使用已编排完成的启动 prompt 开始创作。

func (*Host) Steer

func (h *Host) Steer(text string)

Steer 提交用户干预。

func (*Host) Stream

func (h *Host) Stream() <-chan string

func (*Host) StreamClear

func (h *Host) StreamClear() <-chan struct{}

func (*Host) SwitchModel

func (h *Host) SwitchModel(role, provider, model string) error

type OutlineSnapshot

type OutlineSnapshot struct {
	Chapter   int
	Title     string
	CoreEvent string
}

OutlineSnapshot 是大纲条目的展示摘要。

type TaskSnapshot

type TaskSnapshot struct {
	ID        string
	Kind      string
	Owner     string
	Title     string
	Status    string
	Chapter   int
	Volume    int
	Arc       int
	Summary   string
	Tool      string
	OutputRef string
	UpdatedAt time.Time
}

TaskSnapshot 是任务状态的展示投影(从事件流投影,非事实层)。

type UISnapshot

type UISnapshot struct {
	Provider           string
	NovelName          string
	ModelName          string
	ModelContextWindow int // 当前默认模型的上下文窗口(随 /model 切换实时解析)
	Style              string
	RuntimeState       string // idle / running / pausing / paused / completed
	StatusLabel        string
	Phase              string
	Flow               string
	CurrentChapter     int
	TotalChapters      int
	CompletedCount     int
	TotalWordCount     int
	InProgressChapter  int
	PendingRewrites    []int
	RewriteReason      string
	PendingSteer       string
	RecoveryLabel      string
	IsRunning          bool
	Agents             []AgentSnapshot
	Tasks              []TaskSnapshot

	// 上下文
	ContextTokens         int
	ContextWindow         int
	ContextPercent        float64
	ContextScope          string
	ContextStrategy       string
	ProjectionTokens      int
	ProjectionWindow      int
	ProjectionPercent     float64
	ProjectionStrategy    string
	ProjectionCompacted   int
	ProjectionKept        int
	ContextActiveMessages int
	ContextSummaryCount   int
	ContextCompactedCount int
	ContextKeptCount      int

	// 累计用量(整个会话,跨所有 agent 与模型切换)
	TotalInputTokens      int
	TotalOutputTokens     int
	TotalCacheReadTokens  int
	TotalCacheWriteTokens int
	TotalCostUSD          float64

	// 基础设定
	Premise          string
	Outline          []OutlineSnapshot
	Characters       []string
	Layered          bool
	CurrentVolumeArc string
	NextVolumeTitle  string
	CompassDirection string
	CompassScale     string

	// 详情
	LastCommitSummary  string
	LastReviewSummary  string
	LastCheckpointName string
	RecentSummaries    []string
}

UISnapshot 是 TUI 渲染所需的聚合状态快照。

type UsageTracker added in v0.1.2

type UsageTracker struct {
	// contains filtered or unexported fields
}

UsageTracker 累计整个会话所有 agent 的 LLM 输入/输出 token 与美元成本。

工作机制:

  • 每次 agent 的 OnMessage 回调触发时调用 Record(agentName, msg)
  • agentName 映射到 role(architect_* 归一为 architect),查 ModelSet 当前该 role 绑定的模型
  • 用 models.DefaultRegistry 查模型价格,按非缓存输入/输出/缓存读/缓存写四项累乘
  • 注册表无此模型时,退回 msg.Usage.Cost.Total(provider 自带,可能为 0)
  • 模型热切换(/model)后续消息自动按新模型算价,旧消息保留旧成本

线程安全。

func NewUsageTracker added in v0.1.2

func NewUsageTracker(set *bootstrap.ModelSet) *UsageTracker

func (*UsageTracker) Record added in v0.1.2

func (t *UsageTracker) Record(agentName string, msg agentcore.AgentMessage)

Record 累计一条 agent 消息的 token 与成本。msg 非 Message / Usage 为空时忽略。

func (*UsageTracker) Totals added in v0.1.2

func (t *UsageTracker) Totals() (cost float64, input, output, cacheRead, cacheWrite int)

Totals 返回累计总量的快照。

Directories

Path Synopsis
Package flow 实现垂类路由:Host 根据事实决定下一个调哪个子代理做什么。
Package flow 实现垂类路由:Host 根据事实决定下一个调哪个子代理做什么。
Package imp 实现外部小说章节的导入与反推。
Package imp 实现外部小说章节的导入与反推。
Package reminder 管理 Coordinator / SubAgent 的 StopGuard 与可选的每轮 reminder。
Package reminder 管理 Coordinator / SubAgent 的 StopGuard 与可选的每轮 reminder。

Jump to

Keyboard shortcuts

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