Skip to content
中文

FamilyVersion ​

KIT · interface · Type contract

Immutable snapshot of App state at one point on a branch.

Import ​

ts
import type { FamilyVersion } from '@jangts/agent-core/kits/family';
js
/** @typedef {import('@jangts/agent-core/kits/family').FamilyVersion} FamilyVersion */

Other public entry points:@jangts/agent-core/types, @jangts/agent-core/structs, @jangts/agent-core/kits, @jangts/agent-core/kits/family.

Definition ​

ts
interface FamilyVersion<State>

Type parameters ​

Type parameterConstraintDefault type
State——

Field structure ​

FieldTypeRequiredDescription
idstringRequiredRead-only. Unique version id, generated by the Kit.
parentIdstring | undefinedOptionalRead-only. Version this one was forked or merged from, when lineage applies.
mergeSourceIdstring | undefinedOptionalRead-only. Source version of a merge; present only on versions created by merge.
baseIdstring | undefinedOptionalRead-only. Common ancestor recorded for fork/merge bookkeeping.
branchAgentBranchRequiredRead-only. Core branch snapshot paired with this state.
stateStateRequiredRead-only. App state; treated as immutable by the Kit and snapshotted on write.
createdAtMsnumberRequiredRead-only. Creation instant in epoch milliseconds.

Examples ​

Configure state checks, source verification, and three-way merge ​

Configuration · Node.js

ts
import { defineAgentFamily } from '@jangts/agent-core/kits/family';
import type { FamilyStore } from '@jangts/agent-core/kits/family';
import type { OperationContext } from '@jangts/agent-core';

type State = { goal: string; sourceRevision: string };
export function family<App>(store: FamilyStore<State>,
  authorize: (context: OperationContext<App>) => Promise<void>,
  verify: (state: State) => Promise<void>) {
  return defineAgentFamily<State, App>({
    store, maxVersions: 16, retentionMs: 7 * 86400000,
    isState: (value): value is State => !!value && typeof (value as State).goal === 'string'
      && typeof (value as State).sourceRevision === 'string',
    authorize: (_operation, _record, context) => authorize(context),
    validate: candidate => verify(candidate.state),
    async merge({ base, target, source }) {
      if (target.goal !== base.goal && source.goal !== base.goal && target.goal !== source.goal)
        return { conflicts: ['goal'] };
      return { value: target.goal === base.goal ? source : target, conflicts: [] };
    },
  });
}
js
import { defineAgentFamily } from '@jangts/agent-core/kits/family';
export function family(store, authorize, verify) {
    return defineAgentFamily({
        store, maxVersions: 16, retentionMs: 7 * 86400000,
        isState: (value) => !!value && typeof value.goal === 'string'
            && typeof value.sourceRevision === 'string',
        authorize: (_operation, _record, context) => authorize(context),
        validate: candidate => verify(candidate.state),
        async merge({ base, target, source }) {
            if (target.goal !== base.goal && source.goal !== base.goal && target.goal !== source.goal)
                return { conflicts: ['goal'] };
            return { value: target.goal === base.goal ? source : target, conflicts: [] };
        },
    });
}

Explicitly promote an approved candidate version ​

Extension · Node.js

The App calls this after real approval; promote still re-runs source, authorization, revision, and CAS checks.

ts
import type { AgentFamily } from '@jangts/agent-core/kits/family';
import type { OperationContext } from '@jangts/agent-core';

export async function activate<State, App>(family: AgentFamily<State, App>, id: string,
  candidateId: string, context: OperationContext<App>) {
  const record = await family.read(id, context);
  return family.promote({ id, revision: record.revision, candidateId,
    expectedActiveId: record.activeId }, context);
}
js
export async function activate(family, id, candidateId, context) {
    const record = await family.read(id, context);
    return family.promote({ id, revision: record.revision, candidateId,
        expectedActiveId: record.activeId }, context);
}

API reference, usage examples and developer guides.