Class LeafSelection
java.lang.Object
com.google.genkit.ai.agent.internal.LeafSelection
Utility for selecting the leaf snapshot from a collection of snapshots in a session's
parent-chain.
A leaf is a snapshot whose snapshotId is not referenced as a parentId
by any other snapshot in the list.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <S> SessionSnapshot<S> selectLeaf(List<SessionSnapshot<S>> snapshots, boolean rejectBranching) Selects the leaf snapshot from the given list.
-
Method Details
-
selectLeaf
public static <S> SessionSnapshot<S> selectLeaf(List<SessionSnapshot<S>> snapshots, boolean rejectBranching) Selects the leaf snapshot from the given list.Selection rules:
- Empty list — returns
null. An empty input carries no information about a cycle, so returningnull(no snapshot found) is the correct behaviour. - Exactly one leaf — returns it directly.
- Zero leaves in a non-empty list — the chain is corrupt (cycle). Throws
GenkitExceptionwith error codeFAILED_PRECONDITION. - More than one leaf — if
rejectBranchingistrue, throwsGenkitExceptionwith error codeFAILED_PRECONDITION. Otherwise the most recent leaf is returned (bycreatedAtparsed asInstant;nullcreatedAtis treated as the epoch / earliest). Ties are broken bysnapshotIdin natural (lexicographic) ascending order, taking the greater ID.
- Type Parameters:
S- the type of custom session state- Parameters:
snapshots- the full list of snapshots for a session (may be empty)rejectBranching- iftrue, more than one leaf is treated as an error- Returns:
- the selected leaf snapshot, or
nullif the input list is empty - Throws:
GenkitException- withFAILED_PRECONDITIONwhen the chain contains a cycle or (withrejectBranching=true) multiple leaves
- Empty list — returns
-