asyncmode

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package asyncmode 是「异步对话模式」的纯逻辑:有效模式的判定、降级原因、写给智能体的回执文案。

这里不碰数据库也不碰网络:Store 与 MCP / REST 层把当前状态(用户开关、hook 最近出现的时间)传进来, 拿回判定结果与要写进回执的行。这样判定规则与文案可以脱离其余系统穷举测试,也只有一处定义。

Index

Constants

View Source
const (
	Sync  = "sync"
	Async = "async"
)

模式名。

View Source
const (
	// DefaultWindowAdd 是智能体每发一条新消息、回复窗口增加的时长。
	DefaultWindowAdd = 5 * time.Minute
	// DefaultWaitSlice 是智能体每次用计时器连续等待的最长时间,到点后回头读收件箱。
	DefaultWaitSlice = 10 * time.Minute
	// WindowMax 是回复窗口从此刻起最多能累加到的长度,防止无限续命。
	WindowMax = 30 * time.Minute
)
View Source
const AuthLineMax = 300

AuthLineMax 是 auth: 行的字数上限(含前缀、档位名与等级)。

View Source
const Contract = `` /* 1669-byte string literal not displayed */

Contract 是异步生效时回执里的 async: 契约(不超过 760 字,测试里卡死)。 它是智能体在异步模式下唯一的行为说明,所以把「不可逆操作(L2)批准前绝不执行」(INV-3)写成独立的一条: 用 async_ask 发 L2 确认卡、不阻断其余工作,没有答复就是不批准(#217,Q68 方案 A)。 上限从 600 放宽到 760,是为了写进两条工作方式(#221,用户 #859 / #834,确认项 Q73 选 a): 等用户的事一律发成确认卡、不只写在汇报里((1) 的后半句);长耗时命令默认后台跑、等待用计时器分片((7))。

View Source
const HookFreshness = 60 * time.Minute

HookFreshness 是「最近见过 hook」的有效窗口:异步模式靠 hook 把追加指令与答复在工作期间送到智能体手里, hook 太久没出现,就说明这条通路已经不可靠,此时继续让智能体「发了就走」会让用户的话没人听见,所以自动降级。

Variables

This section is empty.

Functions

func AuthLine

func AuthLine(p Profile) string

AuthLine 把档位写成回执里的 auth: 行;超过 AuthLineMax 时截断规则并以省略号收尾。

func ExtendWindow

func ExtendWindow(until *time.Time, now time.Time, add time.Duration) time.Time

ExtendWindow 在当前截止时间上累加 add(已过期或没有窗口时从 now 起算),整体不超过 now+WindowMax。

func FormatClock

func FormatClock(d time.Duration) string

FormatClock 把时长写成 mm:ss(超过 1 小时写成 h:mm:ss)。

func ReasonText

func ReasonText(r Reason) string

ReasonText 是降级原因的人话(回执与界面共用)。

func ReceiptLines

func ReceiptLines(st State, p Profile) (mode []string, auth string)

ReceiptLines 返回回执头里的 mode 相关行与 auth 行。

  • 没开启过异步的工作流:全部为空(INV-2,回执与升级之前逐字一致);
  • 异步生效:mode: async + 契约,auth 行取代阶段提示词;
  • 开启过、但现在是同步:一行 mode: sync(原因),阶段提示词照旧。

func Signature

func Signature(st State, profileID uint, p Profile) string

Signature 把「智能体该知道的模式状态」压成一个短串:有效模式、降级原因、实际生效的档位(编号 + 内容摘要)。 它变了、而智能体还没被告知,就在下一次工具回执里补一段说明(见 mcp 层的 modeNoticeBlock)。 档位内容也算在内:用户改了档位的规则、换了默认档位、删掉了工作流选用的档位(回落到默认),智能体都该知道。

func WindowLine

func WindowLine(w WindowState, now time.Time, slice time.Duration) string

WindowLine 是回执里的 window: 行。没有窗口时返回空串。

  • 有任务在做:窗口暂停,告诉智能体继续做任务,别为了等而停下来;必须等用户拍板的任务该置「暂缓」, 暂缓的不算「在做」,所以不挡窗口也不挡收尾(「非阻断任务做完才能结束会话」,#124);
  • 窗口还剩时间:手头没别的事就用计时器等 min(剩余, slice),到点重读收件箱;仍无消息且未到期再按剩余等;
  • 窗口已到期:最后读一轮收件箱,仍为空即可收尾。

func WindowRemaining

func WindowRemaining(until *time.Time, now time.Time) (time.Duration, bool)

WindowRemaining 返回窗口剩余时长;没有窗口返回 (0, false);已到期返回 (0, true)。

Types

type Input

type Input struct {
	Enabled  bool      // 用户当前是否开着
	Ever     bool      // 是否曾经开启过
	HookSeen time.Time // 最近一次见到 hook 的时间;零值表示没见过
	Now      time.Time
}

Input 是判定所需的全部事实。

type Profile

type Profile struct {
	Name  string
	Level string
	Rules string
}

Profile 是回执 auth: 行需要的档位信息。

type Reason

type Reason string

Reason 是异步没有生效时的原因(生效时为空)。

const (
	ReasonNone      Reason = ""
	ReasonDisabled  Reason = "disabled"   // 用户开启过,现在关了
	ReasonNoHook    Reason = "no_hook"    // 开着,但这个工作流还没见过 hook
	ReasonHookStale Reason = "hook_stale" // 开着,但 hook 太久没出现
)

type State

type State struct {
	Enabled   bool   // 用户当前是否开着
	Ever      bool   // 是否曾经开启过(开着一定算开启过)
	Effective bool   // 真正生效:开着 且 hook 在线
	Reason    Reason // 没生效的原因;从未开启过的工作流为空
}

State 是判定结果。

func Evaluate

func Evaluate(in Input) State

Evaluate 判定有效模式:用户开启 且 最近一小时内见过 hook 才是异步;否则同步,并给出原因。 从未开启过的工作流不产生原因(Reason 为空)——它没有「降级」可言。

func (State) Mode

func (s State) Mode() string

Mode 返回有效模式名。

type WindowState

type WindowState struct {
	// Until 是走动中的窗口截止时间;暂停(有任务)或没有窗口时为 nil。
	Until *time.Time
	// BankSec 是暂停时攒着的剩余秒数。
	BankSec int
	// ActiveTasks 是进行中 + 验证中的任务数:> 0 时窗口不走。
	ActiveTasks int
}

WindowState 是回复窗口此刻的全部事实。

func (WindowState) Empty

func (w WindowState) Empty() bool

Empty 当前没有任何回复窗口(既不在走,也没有攒着)。

func (WindowState) Paused

func (w WindowState) Paused() bool

Paused 窗口里还攒着时间、但因为有任务在做而没有在走。

Jump to

Keyboard shortcuts

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