Documentation
¶
Overview ¶
Package progress normalizes PMOCK's per-capability task dialects into one customer-facing progress contract.
PMOCK reports progress in at least three different encodings: RPA tasks use 0..4, phone lifecycle operations return a taskStatus plus a phoneStatus, and UI actions document -1/0/1/2 on paper but return 1/2/3 in practice. None of them answers the only question a customer has: what should I do now?
So this package answers exactly that question, with four states:
running nothing to do, wait needs_you reply before the work can continue done read the result (it may carry items that need attention) failed decide whether to retry or change approach
Everything else is progress attached to a state: which step, what it is called, how much longer, and how strong the evidence behind the claim is. The evidence level gates the wording in Copy, so an agent that repeats the canonical sentence verbatim cannot invent progress.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrTimeout = errors.New("timed out")
ErrTimeout means the task did not reach a terminal state before the deadline. The runner still emits a failed event first, so the stream never just stops.
Functions ¶
func Copy ¶
Copy renders the canonical customer-facing sentence for an event.
The rule enforced here is the whole point of the package: the wording may only be as specific as the evidence. Step numbers and percentages appear only when the adapter saw a step; a bare status produces a bare sentence; a heartbeat says out loud that there is nothing new, instead of pretending.
Types ¶
type ActionAdapter ¶
ActionAdapter follows one UI action (click, swipe, input, ...).
The published enum is -1/0/1/2 but the endpoint returns 1/2/3 in practice, the same mismatch waitAction already compensates for. Actions are usually sub-second, so this adapter exists for the occasional long press chain rather than for everyday narration.
func (*ActionAdapter) Poll ¶
func (a *ActionAdapter) Poll(ctx context.Context) (Observation, error)
Poll implements Adapter.
type Adapter ¶
type Adapter interface {
// Kind is the short capability name carried in every event ("rpa").
Kind() string
// Poll reads the current position of the task.
Poll(ctx context.Context) (Observation, error)
}
Adapter turns one PMOCK capability into observations.
Adding a capability means adding an adapter, never a new state: the four states are the contract, the dialects are an implementation detail.
type Counts ¶
Counts is a structured result summary, for example "checked 5, found 1".
It is only set when the flow actually produced the numbers. Copy turns it into "共检查 5 项,1 项需要关注" and nothing else may guess at them.
type Doer ¶
Doer is the slice of client.Client the adapters need. Tests substitute a scripted implementation.
type Event ¶
type Event struct {
Seq int `json:"seq"`
At time.Time `json:"at"`
Kind string `json:"kind"`
Task string `json:"task,omitempty"`
State State `json:"state"`
Step int `json:"step,omitempty"`
Total int `json:"total,omitempty"`
Label string `json:"label,omitempty"`
ETA *int `json:"etaSeconds,omitempty"`
Counts *Counts `json:"counts,omitempty"`
Evidence Evidence `json:"evidence"`
Reason string `json:"reason,omitempty"`
Say string `json:"say"`
Observed map[string]any `json:"observed,omitempty"`
}
Event is one line of the progress stream.
type Evidence ¶
type Evidence string
Evidence is how much the backend actually told us. It decides how specific the copy may be; it is never shown to the customer.
const ( // EvidenceNone means the last poll carried nothing new: a heartbeat. EvidenceNone Evidence = "none" // EvidenceStatus means only an opaque status code. EvidenceStatus Evidence = "status" // EvidenceStep means named business steps, or the failing step. EvidenceStep Evidence = "step" // EvidenceStructured means a structured result, such as a checklist. EvidenceStructured Evidence = "structured" )
type Observation ¶
type Observation struct {
State State
Step int
Total int
Label string
Reason string
Counts *Counts
Observed map[string]any
}
Observation is what one adapter made of a single poll.
Adapters set State and, when they saw one, a step, a label, a reason or a structured result. They never format text: the wording lives in Copy so that every capability sounds the same.
type PhoneAdapter ¶
PhoneAdapter follows a phone lifecycle operation: start, stop, restart, reset or one-click renew. The endpoint needs the phone id and the operate type.
The API answers with two axes (taskStatus and phoneStatus); an operation is only settled when the phone itself reached the expected state. Until then the customer keeps seeing "running", not a premature success.
func (*PhoneAdapter) Poll ¶
func (a *PhoneAdapter) Poll(ctx context.Context) (Observation, error)
Poll implements Adapter.
type RPAAdapter ¶
RPAAdapter follows an RPA task (0 待执行, 1 进行中, 2 成功, 3 部分失败, 4 失败).
RPA is the only capability that can report named business steps, through the node list. When the node list is absent the adapter degrades to a bare status, and Copy degrades with it.
func (*RPAAdapter) Poll ¶
func (a *RPAAdapter) Poll(ctx context.Context) (Observation, error)
Poll implements Adapter.
type Runner ¶
type Runner struct {
Adapter Adapter
Interval time.Duration
Heartbeat time.Duration // negative disables the heartbeat
Timeout time.Duration
Once bool
// Observe turns Run into a long poll: it waits up to this long for the
// state to change, then returns the current event either way. Agents use
// it to narrate a long task without hammering the API or going silent.
Observe time.Duration
Emit func(Event) error
Now func() time.Time
Sleep func(context.Context, time.Duration) bool
}
Runner polls one adapter and emits events when the answer changes.
Three behaviours matter more than the polling itself:
- unchanged polls are silent, so a 10 minute flow does not produce 200 lines that all say "still running";
- silence for longer than Heartbeat produces an explicit "nothing new" event, because a quiet stream and a dead stream look identical to a customer;
- a timeout emits a terminal failed event instead of ending the stream with no explanation.
type State ¶
type State string
State is the customer-facing state of one handed-over piece of work.
The set stays closed on purpose: a state earns its place only when it changes what the customer does next. Two states that lead to the same customer action are one state.
const ( // StateRunning means the work is moving and the customer should wait. StateRunning State = "running" // StateNeedsYou means the work is blocked on a decision only the customer // can make: a price, a permission, a login, a choice between options. StateNeedsYou State = "needs_you" // StateDone means there is a result to read. StateDone State = "done" // StateFailed means there is no usable result. StateFailed State = "failed" )