Package com.tnsai.rag
Class KnowledgeDeclarations
java.lang.Object
com.tnsai.rag.KnowledgeDeclarations
The one reader of
Knowledge declarations.
Every runtime that needs to know which knowledge a role, an agent, an action or a chat turn sees asks here, so the rules live in one place:
- an agent's entries are its
@AgentSpecentries under the agent level of its class configuration (Config): a name the annotation declares is overridden member by member, except that an entry that changesusekeeps amaxTokensorsearchfrom below only when the newuseapplies to it (keepsMaxTokens(com.tnsai.annotations.Knowledge.Use, com.tnsai.annotations.Knowledge.Use),keepsSearch(com.tnsai.annotations.Knowledge.Use, com.tnsai.annotations.Knowledge.Use)), and a name only the file gives follows the annotation entries, in name order; - a role's entries are its
@RoleSpecentries under therolessection of its agent's file, then under its own file, each layered the same way over everything below it, so the role's own file wins and overrides a name only the section gave member by member; - names are unique within one
knowledgelist; use,maxTokens,searchand everySearchmember hold at most one value;- a non-empty
selectapplies only to aFILEorURLsource; - an action's entry names a declared source and sets only
use,maxTokensandsearch; each set value replaces the declared one, and an unsetuseisKnowledge.Use.SEARCH; maxTokensneedsuse = DIRECTandsearchneedsuse = SEARCH, checked on the resolved entry;- action knowledge is valid on
ActionType.LLMandActionType.LOCALonly; - the SEARCH sources of one scope merge their
Searchsettings member by member, and two different values for one member fail; - an action names at most one
GRAPHentry and no document entry beside it; a wider scope merges its document entries without its GRAPH entries, and a scope of GRAPH entries alone searches with strategy GRAPH unless a set strategy says otherwise, whichKnowledgeEntryrejects; MEMORYknowledge is chat knowledge the application supplies at runtime: only@AgentSpecdeclares it, no action names it, and an agent's chat scope holds at most one MEMORY entry and no other SEARCH entry beside it; DIRECT entries may sit beside it.- a
KnowledgePolicynames a source the knowledge of its own spec declares, configuration files layered, and a source has at most one;WHEN_CONFIGUREDapplies to aFILEsource whoseuseis notDIRECT.
Violations throw IllegalStateException naming the declaration.
- Since:
- 0.18.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic List<KnowledgeEntry> chatScope(List<KnowledgeEntry> entries) static RetrievalConfig.BuilderThe settings a chat turn's SEARCH starts from: chat keeps every hit and lets the model judge relevance (minScore 0), caches, and grounds without evidence rather than failing the turn.static List<KnowledgeEntry> Knowledge a role or agent class declares, in declaration order.static List<KnowledgeEntry> declared(Class<?> host, List<KnowledgeEntry> overrides, String overridesWhere) The knowledge an agent class chats and acts with: what its@AgentSpecdeclares, asdeclared(Class)reads it, withoverrideslayered over it by name.static booleandeclaresKnowledge(Class<?> host) static KnowledgeSelectionforAction(Method method, List<KnowledgeEntry> available) The knowledge an action sees: the sources itsActionSpec.knowledge()names, with the action's values applied.static KnowledgeSelectionLikeforAction(Method, List), but an action the agent gives its knowledge by name, keyed by action name inactionKnowledge, sees those entries as declared instead of itsActionSpec.knowledge().static Optional<KnowledgeSelection> forActionIfResolvable(Method method, List<KnowledgeEntry> available) LikeforAction(Method, List), but leaves an action that names knowledge outsideavailableunresolved instead of failing.static Optional<KnowledgeSelection> forActionIfResolvable(Method method, List<KnowledgeEntry> available, Map<String, List<String>> actionKnowledge) LikeforActionIfResolvable(Method, List), with the action's knowledge given by name asforAction(Method, List, Map)takes it.static booleanhasKnowledge(Method method) static booleanLikehasKnowledge(Method), with the knowledge of some actions given by name: an action given names has knowledge when it is given at least one, whatever itsActionSpec.knowledge()declares.static booleankeepsMaxTokens(Knowledge.Use below, Knowledge.Use use) Whether a layer keeps themaxTokensof the layer below it when the layer does not givemaxTokensitself: when it leavesuseas it was, or changes it toKnowledge.Use.DIRECT.static booleankeepsSearch(Knowledge.Use below, Knowledge.Use use) Whether a layer keeps thesearchof the layer below it when the layer does not givesearchitself: when it leavesuseas it was, or changes it toKnowledge.Use.SEARCH.static Optional<KnowledgePolicy> The presence policy a role or agent class gives a source it declares: theKnowledgePolicyof that name in theknowledgePolicyof the spec whose knowledge, configuration files layered, declares the source.static voidrequireUniqueRoleNames(Collection<Class<?>> roleClasses, String agent) Checks that no two roles of one agent share a name.static StringThe name a role is referenced by:RoleSpec.name(), or the simple class name when that is blank.static KnowledgeSelectionselect(List<KnowledgeEntry> entries, String where) A scope over the given entries, such as a chat turn over all of an agent's knowledge.static KnowledgeSelectionselect(List<KnowledgeEntry> entries, String where, Supplier<RetrievalConfig.Builder> defaults) A scope over the given entries whose SEARCH settings overlay the given defaults instead of the framework's, such as a chat turn's.static voidvalidate(Class<?> host, List<KnowledgeEntry> outer) Checks every action of a class against the knowledge in its reach.static voidLikevalidate(Class, List), with the knowledge of some actions given by name asforAction(Method, List, Map)takes it.static voidvalidateAction(Method method) Checks what an action declares on its own: its type allows knowledge, each entry names a source once and sets no source member, and every member holds at most one value.static StringWhere a class declares its knowledge as a whole, for the rules over the whole list:@AgentSpec onthe class,@RoleSpec onit, or@RoleSpec and @AgentSpec ona class that is both, thenwitheach configuration file that gives it knowledge.static StringWhere an agent class's knowledge is declared whenoverridesWheregives entries over it, asdeclared(Class, List, String)layers them:where(Class)andoverridesWhere, oroverridesWherealone for a class without@AgentSpec.
-
Method Details
-
declared
Knowledge a role or agent class declares, in declaration order.- Parameters:
host- a class carryingRoleSpecorAgentSpec- Returns:
- the declared entries, each resolved on its own
- Throws:
IllegalStateException- when a declaration breaks a rule
-
declared
public static List<KnowledgeEntry> declared(Class<?> host, List<KnowledgeEntry> overrides, String overridesWhere) The knowledge an agent class chats and acts with: what its@AgentSpecdeclares, asdeclared(Class)reads it, withoverrideslayered over it by name. An override replaces the declared entry of its name in place, so one that replaces an entry of the class's@RoleSpecstays a role entry; an override with a new name follows the declared entries. A class without@AgentSpecdeclares no agent knowledge, so its knowledge is the overrides alone. The rules over the whole list run on the result.- Parameters:
host- the agent classoverrides- the entries that win over the declared onesoverridesWhere- what gives the overrides, for messages- Returns:
- the layered entries
- Throws:
IllegalStateException- when a declaration or the layered list breaks a rule- Since:
- 0.18.0
-
policy
The presence policy a role or agent class gives a source it declares: theKnowledgePolicyof that name in theknowledgePolicyof the spec whose knowledge, configuration files layered, declares the source.declared(Class)validates the policies.- Parameters:
host- a role or agent classname- the source name- Returns:
- the policy, or empty when the source has none and so must load
- Since:
- 0.18.1
-
validateAction
Checks what an action declares on its own: its type allows knowledge, each entry names a source once and sets no source member, and every member holds at most one value. Names are resolved later, against the knowledge in reach, byforAction(Method, List).- Parameters:
method- an action method- Throws:
IllegalStateException- when the action's knowledge breaks a rule
-
forAction
The knowledge an action sees: the sources itsActionSpec.knowledge()names, with the action's values applied.- Parameters:
method- an action methodavailable- the knowledge in reach of the action; a later entry with the same name replaces an earlier one- Returns:
- the action's scope;
KnowledgeSelection.EMPTYwhen it names none - Throws:
IllegalStateException- when a name is unknown or the resolved scope breaks a rule
-
forAction
public static KnowledgeSelection forAction(Method method, List<KnowledgeEntry> available, Map<String, List<String>> actionKnowledge) LikeforAction(Method, List), but an action the agent gives its knowledge by name, keyed by action name inactionKnowledge, sees those entries as declared instead of itsActionSpec.knowledge().- Parameters:
method- an action methodavailable- the knowledge in reach of the actionactionKnowledge- entry names by action name that replace an action's own knowledge- Returns:
- the action's scope;
KnowledgeSelection.EMPTYwhen it names none - Throws:
IllegalStateException- when a name is unknown or the resolved scope breaks a rule- Since:
- 0.18.0
-
forActionIfResolvable
public static Optional<KnowledgeSelection> forActionIfResolvable(Method method, List<KnowledgeEntry> available) LikeforAction(Method, List), but leaves an action that names knowledge outsideavailableunresolved instead of failing. A role resolves its actions this way before it knows its agent; the agent resolves them strictly.- Parameters:
method- an action methodavailable- the knowledge in reach so far- Returns:
- the action's scope, or empty when a name is not in reach yet
- Throws:
IllegalStateException- when the resolved scope breaks a rule
-
forActionIfResolvable
public static Optional<KnowledgeSelection> forActionIfResolvable(Method method, List<KnowledgeEntry> available, Map<String, List<String>> actionKnowledge) LikeforActionIfResolvable(Method, List), with the action's knowledge given by name asforAction(Method, List, Map)takes it.- Parameters:
method- an action methodavailable- the knowledge in reach so faractionKnowledge- entry names by action name that replace an action's own knowledge- Returns:
- the action's scope, or empty when a name is not in reach yet
- Throws:
IllegalStateException- when the resolved scope breaks a rule- Since:
- 0.18.0
-
select
A scope over the given entries, such as a chat turn over all of an agent's knowledge.- Parameters:
entries- the sources in scope, in declaration orderwhere- what declared them, for messages- Returns:
- the scope
- Throws:
IllegalStateException- when the SEARCH settings conflict
-
select
public static KnowledgeSelection select(List<KnowledgeEntry> entries, String where, Supplier<RetrievalConfig.Builder> defaults) A scope over the given entries whose SEARCH settings overlay the given defaults instead of the framework's, such as a chat turn's.- Parameters:
entries- the sources in scope, in declaration orderwhere- what declared them, for messagesdefaults- a fresh builder carrying the scope's defaults per call- Returns:
- the scope
- Throws:
IllegalStateException- when the SEARCH settings conflict- Since:
- 0.18.0
-
validate
Checks every action of a class against the knowledge in its reach.- Parameters:
host- a role or agent classouter- knowledge in reach beyond the class's own declarations, such as the enclosing agent's; it replaces a class entry of the same name- Throws:
IllegalStateException- when a declaration breaks a rule
-
validate
public static void validate(Class<?> host, List<KnowledgeEntry> outer, Map<String, List<String>> actionKnowledge) Likevalidate(Class, List), with the knowledge of some actions given by name asforAction(Method, List, Map)takes it.- Parameters:
host- a role or agent classouter- knowledge in reach beyond the class's own declarationsactionKnowledge- entry names by action name that replace an action's own knowledge- Throws:
IllegalStateException- when a declaration breaks a rule- Since:
- 0.18.0
-
hasKnowledge
- Parameters:
method- an action method- Returns:
truewhen the action names knowledge
-
hasKnowledge
LikehasKnowledge(Method), with the knowledge of some actions given by name: an action given names has knowledge when it is given at least one, whatever itsActionSpec.knowledge()declares.- Parameters:
method- an action methodactionKnowledge- entry names by action name that replace an action's own knowledge- Returns:
truewhen the action has knowledge- Since:
- 0.18.0
-
declaresKnowledge
- Parameters:
host- a role or agent class- Returns:
truewhen the class declares knowledge or one of its actions names some
-
where
Where a class declares its knowledge as a whole, for the rules over the whole list:@AgentSpec onthe class,@RoleSpec onit, or@RoleSpec and @AgentSpec ona class that is both, thenwitheach configuration file that gives it knowledge. A file counts on the side whose annotation names it. The chat scope and the merged search of a class are such rules.- Parameters:
host- the declaring class- Returns:
- the label
- Since:
- 0.18.0
-
where
Where an agent class's knowledge is declared whenoverridesWheregives entries over it, asdeclared(Class, List, String)layers them:where(Class)andoverridesWhere, oroverridesWherealone for a class without@AgentSpec.- Parameters:
host- the agent classoverridesWhere- what gives the overrides- Returns:
- the label
- Since:
- 0.18.0
-
chatScope
The knowledge a chat turn sees: the enabled entries whose use isSEARCHorDIRECT, in declaration order. SEQUENTIAL entries are ignored by chat, andGRAPHentries stay out of it; actions that name them still see them.- Parameters:
entries- an agent's@AgentSpecknowledge- Returns:
- the entries chat sees
-
chatSearchDefaults
The settings a chat turn's SEARCH starts from: chat keeps every hit and lets the model judge relevance (minScore 0), caches, and grounds without evidence rather than failing the turn. A field the entries'@Searchsets wins over these.- Returns:
- a fresh builder carrying the chat defaults
- Since:
- 0.18.0
-
roleName
The name a role is referenced by:RoleSpec.name(), or the simple class name when that is blank. An anonymous class has no simple name and is referenced by its binary name.- Parameters:
roleClass- a role class- Returns:
- the role name
-
requireUniqueRoleNames
Checks that no two roles of one agent share a name.- Parameters:
roleClasses- the agent's role classesagent- the agent, for messages- Throws:
IllegalStateException- when two roles share a name
-
keepsMaxTokens
Whether a layer keeps themaxTokensof the layer below it when the layer does not givemaxTokensitself: when it leavesuseas it was, or changes it toKnowledge.Use.DIRECT. SeekeepsSearch(com.tnsai.annotations.Knowledge.Use, com.tnsai.annotations.Knowledge.Use)for why a layer that changesusedrops the members of the other modes.- Parameters:
below- the resolved use of the layer below; unset isKnowledge.Use.SEARCHuse- the resolved use of the layer- Returns:
truewhen themaxTokensbelow carries into the layer
-
keepsSearch
Whether a layer keeps thesearchof the layer below it when the layer does not givesearchitself: when it leavesuseas it was, or changes it toKnowledge.Use.SEARCH. A layer that changesusedrops the members of every mode other than its new use: it could not unset them, so inheriting them would make the combination unwritable. A layer that leavesuseas it was inherits every member, so a lower declaration that sets a member itsusedoes not take still fails. An action over its declared source and a configuration file over the layer below it both layer this way.- Parameters:
below- the resolved use of the layer below; unset isKnowledge.Use.SEARCHuse- the resolved use of the layer- Returns:
truewhen thesearchbelow carries into the layer
-