Skip to content

Operations ​

Every change to a plan is a batch of operations from one actor:

ts
import { applyPlanOps } from '@adecore/plan';

const result = applyPlanOps(plan, [{ op: 'set', ids: ['tests'], state: 'done', next: 'publish' }], {
    actor: 'agent',
    now: new Date().toISOString()
});

if (result.ok) {
    save(result.plan);
} else {
    reply(result.code, result.message);
}

applyPlanOps(plan, ops, options) works on a copy, and each operation sees the ones before it. One refusal leaves your plan untouched, and a batch that passes raises rev by one.

options is a PlanApplyOptions: the actor, the now written into at, and an optional mintId. A success is a PlanApplied, { ok: true, plan, minted, dropped }. minted lists the ids made for new items. dropped lists the steps that lost their state when they got their first sub-step.

The operations ​

opFieldsDoes
setids, state; optional note, nextSets the state of leaf steps. next marks one more step active.
noteid, textWrites the note of a step. An empty text clears it.
unlockids, or 'all'Makes every step only the agent checks, in those subtrees, a step anyone checks.
addtype, title; optional id, description, checks, under, afterAdds a section, text block or step.
editid; optional title, description, checksChanges an item. An empty description removes it.
moveid; optional under, afterMoves an item with everything under it.
removeidRemoves an item with everything under it.
metaOptional title, summary, status, checksChanges the plan's meta. An empty summary or status removes it.

under names the section or step to put an item in, and after the item it follows there. Without either the item goes last at the top. With only after it goes right after that item. A section only stands at the top, a text block never under a step.

A set only takes leaf steps. Several steps may be active at once; next does not end the others.

PlanOpSchema parses any operation and PlanPersonOpSchema only the three a person may send. Who may send which, and on which steps, is on Permissions.

Building a plan in one batch ​

Since each operation sees the ones before it, an agent can add a section and fill it in one batch:

ts
applyPlanOps(
    plan,
    [
        { op: 'add', type: 'section', id: 'release', title: 'Release' },
        { op: 'add', type: 'step', id: 'approve', title: 'Approve the release', checks: 'person', under: 'release' },
        { op: 'add', type: 'step', id: 'publish', title: 'Publish to npm', under: 'release', after: 'approve' },
        { op: 'meta', status: 'Waiting for approval' }
    ],
    { actor: 'agent', now: new Date().toISOString() }
);

Refusals ​

A refusal is a PlanRefusal, { ok: false, code, message }. The message names the item; pass it to the agent so it can correct its next batch. PlanVerdict is a refusal or { ok: true }, and refuse(code, message) builds one for the app's own checks.

PlanRefusalCodeWhen
op-not-allowedThe actor may not send this operation, or a person sent next
step-lockedA person set a step only the agent checks
person-onlyThe agent touched a step only a person checks
set-by-personThe agent would change or remove what a person set
unlocked-by-personThe agent tried to lock a step a person unlocked
plan-missing-itemAn id names no item
plan-not-a-stepA step operation named a section or text block
plan-parent-stateA state was set on, or stored on, a step with sub-steps
plan-bad-positionunder or after puts an item where it cannot stand
duplicate-idAn id is taken
plan-too-deepSteps nest deeper than PLAN_LIMITS.depth
plan-too-largeThe plan holds more items than PLAN_LIMITS.items
plan-invalidThe operations, the draft or the result do not fit the schema
plan-not-foundNot returned by the package: for an app that looks plans up
too-many-plansNot returned by the package: for an app that limits plans per conversation

In a store ​

Operations carry no base revision, so a store applies each batch to the latest plan inside a transaction, or one queue per plan:

ts
import { applyPlanOps, refuse, type PlanApplied, type PlanRefusal } from '@adecore/plan';
import type { PlanActor, PlanOp } from '@adecore/plan/protocol';

async function update(id: string, ops: PlanOp[], actor: PlanActor): Promise<PlanApplied | PlanRefusal> {
    return store.transaction(async (latest) => {
        const plan = await latest(id);

        return plan ? applyPlanOps(plan, ops, { actor, now: new Date().toISOString() }) : refuse('plan-not-found', `No plan "${id}"`);
    });
}

store is the app's storage; it saves result.plan only when the result is ok. Tell the page after the commit. A custom mintId may run for a batch that is refused later, so it must not write anything.

Licenses and third-party notices are listed in each package.