Package com.tnsai.rag
Class RetrievedContextRenderer
java.lang.Object
com.tnsai.rag.RetrievedContextRenderer
Renders retrieved documents behind a single, explicit untrusted-context boundary.
Document content and provenance are data controlled by an external knowledge
source. This renderer XML-escapes both before placing them inside the
<tnsai-memory> fence, preventing retrieved text from terminating the
boundary or introducing sibling prompt sections. The original user message is
kept outside the fence and remains byte-for-byte unchanged.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringSystem-level instruction shown exactly once whenever retrieved evidence is rendered. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringFences an already assembled action context as untrusted data.static Stringrender(String userMessage, ChatKnowledgeContext.Turn turn, RetrievalResult result, int contextWindow) Renders a chat turn's knowledge behind one boundary: the blocks declared before the first SEARCH entry, the SEARCH evidence within the context window, then the blocks declared after it.static Stringrender(String userMessage, RetrievalResult result, int contextWindow) Renders canonical retrieval evidence within the configured context window.static Stringrender(String userMessage, List<SearchResult> results) Renders search results for a single outbound LLM request.static List<KnowledgeUsage> searchUsage(List<String> sources, RetrievalResult result, int contextWindow) Says what each SEARCH source gave whenresultis rendered withincontextWindow: the evidence that fits, as therendermethods cut it, attributed to its source.static StringwithUntrustedContextPolicy(String systemPrompt) Adds the canonical untrusted-context policy to a request system prompt.
-
Field Details
-
UNTRUSTED_CONTEXT_DISCLOSURE
System-level instruction shown exactly once whenever retrieved evidence is rendered.- See Also:
-
-
Method Details
-
render
Renders search results for a single outbound LLM request.- Parameters:
userMessage- the original user messageresults- retrieved evidence; may benullor empty- Returns:
- a fenced request, or the original message when no evidence can be rendered
-
render
Renders canonical retrieval evidence within the configured context window.- Parameters:
userMessage- original user messageresult- canonical retrieval resultcontextWindow- maximum estimated tokens devoted to evidence- Returns:
- fenced evidence followed by the unchanged user message
-
render
public static String render(String userMessage, ChatKnowledgeContext.Turn turn, RetrievalResult result, int contextWindow) Renders a chat turn's knowledge behind one boundary: the blocks declared before the first SEARCH entry, the SEARCH evidence within the context window, then the blocks declared after it.Each block is escaped as a whole, as
fence(String)does, because it was assembled before rendering.- Parameters:
userMessage- original user messageturn- the turn's DIRECT blocksresult- SEARCH evidence, ornullwhen nothing was searchedcontextWindow- maximum estimated tokens devoted to the evidence- Returns:
- fenced knowledge followed by the unchanged user message, or the original message when nothing can be rendered
- Since:
- 0.18.0
-
searchUsage
public static List<KnowledgeUsage> searchUsage(List<String> sources, RetrievalResult result, int contextWindow) Says what each SEARCH source gave whenresultis rendered withincontextWindow: the evidence that fits, as therendermethods cut it, attributed to its source. With one source, every fitted item is that source's; with several, each item goes to the source its provenance names. Tokens are the sum of each fitted item's estimate.- Parameters:
sources- the SEARCH sources in declaration order, one row each; when empty, one row per provenance source, in the order the evidence first names itresult- the search result, ornullwhen nothing was searchedcontextWindow- maximum estimated tokens devoted to the evidence- Returns:
- one SEARCH row per source
- Since:
- 0.18.0
-
withUntrustedContextPolicy
Adds the canonical untrusted-context policy to a request system prompt.- Parameters:
systemPrompt- the request's base system prompt- Returns:
- the prompt containing the policy exactly once
-
fence
Fences an already assembled action context as untrusted data.Action retrieval supports caller-defined context templates, so the original document boundaries are no longer available at LLM dispatch. Escaping the complete assembled value preserves that formatting as data while preventing it from closing the canonical memory boundary.
- Parameters:
context- assembled retrieved context- Returns:
- escaped context inside the canonical untrusted boundary
-