Skip to content
English

FamilyDefinition ​

KIT · interface · 类型合同

App-owned hooks and limits for one family.

导入 ​

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

其他公开入口:@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/kits、@jangts/agent-core/kits/family。

定义 ​

ts
interface FamilyDefinition<State, App>

泛型参数 ​

类型参数约束默认类型
State——
App——

字段结构 ​

字段类型必需说明
storeFamilyStore<State>必需只读。见类型定义及调用场景。
maxVersionsnumber | undefined可省略只读。Maximum retained versions; older versions are dropped once the bound is exceeded.
retentionMsnumber | undefined可省略只读。Retention window in milliseconds, measured from a write.
now(() => number) | undefined可省略只读。Clock override for timestamps and expiry; defaults to Date.now.
isState(value: unknown) => value is State必需Validates a state loaded from the store before it is used.
authorize(operation: FamilyOperation, record: FamilyRecord<State> | undefined, context: OperationContext<App>) => MaybePromise<void>必需Guards one mutation; called before any write, including with an absent record.
validate(candidate: FamilyVersion<State>, context: OperationContext<App>) => MaybePromise<void>必需Validates a candidate version before it is appended.
merge((input: { base: State; target: State; source: State; }, context: OperationContext<App>) => MaybePromise<{ value?: State; conflicts: readonly string[]; }>) | undefined可省略Resolves a three-way merge. Returns the merged state and any conflicts. A non-empty conflict list aborts the merge without writing a version, so value is ignored in that case.

成员目录 ​

成员用途
isStateValidates a state loaded from the store before it is used.
authorizeGuards one mutation; called before any write, including with an absent record.
validateValidates a candidate version before it is appended.

成员详情 ​

isState ​

Validates a state loaded from the store before it is used.

ts
FamilyDefinition.isState(value: unknown): value is State

Validates a state loaded from the store before it is used.

参数类型传递说明
valueunknown必需—

返回: boolean。

authorize ​

Guards one mutation; called before any write, including with an absent record.

ts
FamilyDefinition.authorize(operation: FamilyOperation, record: FamilyRecord<State> | undefined, context: OperationContext<App>): MaybePromise<void>

Guards one mutation; called before any write, including with an absent record.

参数类型传递说明
operation"create" | "fork" | "merge" | "promote"必需—
recordFamilyRecord<State> | undefined必需—
contextOperationContext<App>必需—
context 的字段
字段类型必需说明
appunknown extends App ? App : Readonly<App>必需只读。见类型定义及调用场景。
loggerSubFlowLog | undefined可省略只读。Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignal必需只读。Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefined可省略只读。Diagnostic scope of the enclosing call, when recording is enabled.

参数 record的可选结构: 选择其中一种结构,各分支的字段要求分别列出。

分支 1

undefined

分支 2
字段类型必需说明
idstring必需只读。Family id; also the store key.
revisionnumber必需只读。Optimistic concurrency token; every accepted mutation increments it by one.
activeIdstring必需只读。Id of the active version; promotion moves this pointer.
expiresAtMsnumber必需只读。Instant after which the record is no longer retained, in epoch milliseconds.
versionsreadonly FamilyVersion<State>[]必需只读。Versions in creation order; bounded by maxVersions when configured.

返回: MaybePromise<void>。

等待后的值: void。

validate ​

Validates a candidate version before it is appended.

ts
FamilyDefinition.validate(candidate: FamilyVersion<State>, context: OperationContext<App>): MaybePromise<void>

Validates a candidate version before it is appended.

参数类型传递说明
candidateFamilyVersion<State>必需—
contextOperationContext<App>必需—
candidate 的字段
字段类型必需说明
idstring必需只读。Unique version id, generated by the Kit.
parentIdstring | undefined可省略只读。Version this one was forked or merged from, when lineage applies.
mergeSourceIdstring | undefined可省略只读。Source version of a merge; present only on versions created by merge.
baseIdstring | undefined可省略只读。Common ancestor recorded for fork/merge bookkeeping.
branchAgentBranch必需只读。Core branch snapshot paired with this state.
stateState必需只读。App state; treated as immutable by the Kit and snapshotted on write.
createdAtMsnumber必需只读。Creation instant in epoch milliseconds.
context 的字段
字段类型必需说明
appunknown extends App ? App : Readonly<App>必需只读。见类型定义及调用场景。
loggerSubFlowLog | undefined可省略只读。Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignal必需只读。Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefined可省略只读。Diagnostic scope of the enclosing call, when recording is enabled.

返回: MaybePromise<void>。

等待后的值: void。

使用示例 ​

配置状态检查、来源验证和三方合并 ​

配置 · 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: [] };
        },
    });
}

显式启用已批准的候选版本 ​

扩展 · Node.js

App 在真实审批后调用;promote 仍重新执行来源、授权、版本和 CAS 检查。

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);
}

相关策略源码 ​

相关定义 ​

接口参考、使用示例与开发指南。