Skip to content
中文

AgentFamily ​

KIT · interface · Type contract

Compiled family API. Every mutation returns the record after the write.

Import ​

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

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

Definition ​

ts
interface AgentFamily<State, App>

Type parameters ​

Type parameterConstraintDefault type
State——
App——

Field structure ​

FieldTypeRequiredDescription
read(id: string, context: OperationContext<App>) => Promise<FamilyRecord<State>>RequiredReads the record; throws when the family does not exist or has expired.
create(request: { id: string; branch: AgentBranch; state: State; }, context: OperationContext<App>) => Promise<FamilyRecord<State>>RequiredCreates the first version and makes it active.
fork(request: { id: string; revision: number; parentId: string; branch: AgentBranch; state: State; }, context: OperationContext<App>) => Promise<FamilyRecord<State>>RequiredAppends a version forked from parentId; the active pointer does not change.
merge(request: { id: string; revision: number; baseId: string; targetId: string; sourceId: string; }, context: OperationContext<App>) => Promise<{ saved: boolean; record: FamilyRecord<State>; conflicts: readonly string[]; }>RequiredMerges source into target; saved is false when the merge reported conflicts.
promote(request: { id: string; revision: number; candidateId: string; expectedActiveId: string; }, context: OperationContext<App>) => Promise<FamilyRecord<State>>RequiredMakes candidateId active; fails when the active version changed since expectedActiveId.

Member index ​

MemberPurpose
readReads the record; throws when the family does not exist or has expired.
createCreates the first version and makes it active.
forkAppends a version forked from parentId; the active pointer does not change.
mergeMerges source into target; saved is false when the merge reported conflicts.
promoteMakes candidateId active; fails when the active version changed since expectedActiveId.

Member details ​

read ​

Reads the record; throws when the family does not exist or has expired.

ts
AgentFamily.read(id: string, context: OperationContext<App>): Promise<FamilyRecord<State>>

Reads the record; throws when the family does not exist or has expired.

ParameterTypePassingDescription
idstringRequired—
contextOperationContext<App>Required—
Fields of context
FieldTypeRequiredDescription
appunknown extends App ? App : Readonly<App>RequiredRead-only. See the type definition and usage context.
loggerSubFlowLog | undefinedOptionalRead-only. Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignalRequiredRead-only. Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Diagnostic scope of the enclosing call, when recording is enabled.

Returns: Promise<FamilyRecord<State>>.

Awaited value: FamilyRecord<State>.

Return value structure
FieldTypeRequiredDescription
idstringRequiredRead-only. Family id; also the store key.
revisionnumberRequiredRead-only. Optimistic concurrency token; every accepted mutation increments it by one.
activeIdstringRequiredRead-only. Id of the active version; promotion moves this pointer.
expiresAtMsnumberRequiredRead-only. Instant after which the record is no longer retained, in epoch milliseconds.
versionsreadonly FamilyVersion<State>[]RequiredRead-only. Versions in creation order; bounded by maxVersions when configured.

create ​

Creates the first version and makes it active.

ts
AgentFamily.create(request: { id: string; branch: AgentBranch; state: State; }, context: OperationContext<App>): Promise<FamilyRecord<State>>

Creates the first version and makes it active.

ParameterTypePassingDescription
request{ id: string; branch: AgentBranch; state: State; }Required—
contextOperationContext<App>Required—
Fields of request
FieldTypeRequiredDescription
idstringRequiredSee the type definition and usage context.
branchAgentBranchRequiredSee the type definition and usage context.
stateStateRequiredSee the type definition and usage context.
Fields of context
FieldTypeRequiredDescription
appunknown extends App ? App : Readonly<App>RequiredRead-only. See the type definition and usage context.
loggerSubFlowLog | undefinedOptionalRead-only. Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignalRequiredRead-only. Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Diagnostic scope of the enclosing call, when recording is enabled.

Returns: Promise<FamilyRecord<State>>.

Awaited value: FamilyRecord<State>.

Return value structure
FieldTypeRequiredDescription
idstringRequiredRead-only. Family id; also the store key.
revisionnumberRequiredRead-only. Optimistic concurrency token; every accepted mutation increments it by one.
activeIdstringRequiredRead-only. Id of the active version; promotion moves this pointer.
expiresAtMsnumberRequiredRead-only. Instant after which the record is no longer retained, in epoch milliseconds.
versionsreadonly FamilyVersion<State>[]RequiredRead-only. Versions in creation order; bounded by maxVersions when configured.

fork ​

Appends a version forked from parentId; the active pointer does not change.

ts
AgentFamily.fork(request: { id: string; revision: number; parentId: string; branch: AgentBranch; state: State; }, context: OperationContext<App>): Promise<FamilyRecord<State>>

Appends a version forked from parentId; the active pointer does not change.

ParameterTypePassingDescription
request{ id: string; revision: number; parentId: string; branch: AgentBranch; state: State; }Required—
contextOperationContext<App>Required—
Fields of request
FieldTypeRequiredDescription
idstringRequiredSee the type definition and usage context.
revisionnumberRequiredSee the type definition and usage context.
parentIdstringRequiredSee the type definition and usage context.
branchAgentBranchRequiredSee the type definition and usage context.
stateStateRequiredSee the type definition and usage context.
Fields of context
FieldTypeRequiredDescription
appunknown extends App ? App : Readonly<App>RequiredRead-only. See the type definition and usage context.
loggerSubFlowLog | undefinedOptionalRead-only. Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignalRequiredRead-only. Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Diagnostic scope of the enclosing call, when recording is enabled.

Returns: Promise<FamilyRecord<State>>.

Awaited value: FamilyRecord<State>.

Return value structure
FieldTypeRequiredDescription
idstringRequiredRead-only. Family id; also the store key.
revisionnumberRequiredRead-only. Optimistic concurrency token; every accepted mutation increments it by one.
activeIdstringRequiredRead-only. Id of the active version; promotion moves this pointer.
expiresAtMsnumberRequiredRead-only. Instant after which the record is no longer retained, in epoch milliseconds.
versionsreadonly FamilyVersion<State>[]RequiredRead-only. Versions in creation order; bounded by maxVersions when configured.

merge ​

Merges source into target; saved is false when the merge reported conflicts.

ts
AgentFamily.merge(request: { id: string; revision: number; baseId: string; targetId: string; sourceId: string; }, context: OperationContext<App>): Promise<{ saved: boolean; record: FamilyRecord<State>; conflicts: readonly string[]; }>

Merges source into target; saved is false when the merge reported conflicts.

ParameterTypePassingDescription
request{ id: string; revision: number; baseId: string; targetId: string; sourceId: string; }Required—
contextOperationContext<App>Required—
Fields of request
FieldTypeRequiredDescription
idstringRequiredSee the type definition and usage context.
revisionnumberRequiredSee the type definition and usage context.
baseIdstringRequiredSee the type definition and usage context.
targetIdstringRequiredSee the type definition and usage context.
sourceIdstringRequiredSee the type definition and usage context.
Fields of context
FieldTypeRequiredDescription
appunknown extends App ? App : Readonly<App>RequiredRead-only. See the type definition and usage context.
loggerSubFlowLog | undefinedOptionalRead-only. Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignalRequiredRead-only. Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Diagnostic scope of the enclosing call, when recording is enabled.

Returns: Promise<{ saved: boolean; record: FamilyRecord<State>; conflicts: readonly string[]; }>.

Awaited value: { saved: boolean; record: FamilyRecord<State>; conflicts: readonly string[]; }.

Return value structure
FieldTypeRequiredDescription
savedbooleanRequiredSee the type definition and usage context.
recordFamilyRecord<State>RequiredSee the type definition and usage context.
conflictsreadonly string[]RequiredSee the type definition and usage context.

promote ​

Makes candidateId active; fails when the active version changed since expectedActiveId.

ts
AgentFamily.promote(request: { id: string; revision: number; candidateId: string; expectedActiveId: string; }, context: OperationContext<App>): Promise<FamilyRecord<State>>

Makes candidateId active; fails when the active version changed since expectedActiveId.

ParameterTypePassingDescription
request{ id: string; revision: number; candidateId: string; expectedActiveId: string; }Required—
contextOperationContext<App>Required—
Fields of request
FieldTypeRequiredDescription
idstringRequiredSee the type definition and usage context.
revisionnumberRequiredSee the type definition and usage context.
candidateIdstringRequiredSee the type definition and usage context.
expectedActiveIdstringRequiredSee the type definition and usage context.
Fields of context
FieldTypeRequiredDescription
appunknown extends App ? App : Readonly<App>RequiredRead-only. See the type definition and usage context.
loggerSubFlowLog | undefinedOptionalRead-only. Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignalRequiredRead-only. Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Diagnostic scope of the enclosing call, when recording is enabled.

Returns: Promise<FamilyRecord<State>>.

Awaited value: FamilyRecord<State>.

Return value structure
FieldTypeRequiredDescription
idstringRequiredRead-only. Family id; also the store key.
revisionnumberRequiredRead-only. Optimistic concurrency token; every accepted mutation increments it by one.
activeIdstringRequiredRead-only. Id of the active version; promotion moves this pointer.
expiresAtMsnumberRequiredRead-only. Instant after which the record is no longer retained, in epoch milliseconds.
versionsreadonly FamilyVersion<State>[]RequiredRead-only. Versions in creation order; bounded by maxVersions when configured.

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.