Package com.tnsai.rag

Class RetrievedContextRenderer

java.lang.Object
com.tnsai.rag.RetrievedContextRenderer

public final class RetrievedContextRenderer extends Object
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 Details

    • UNTRUSTED_CONTEXT_DISCLOSURE

      public static final String UNTRUSTED_CONTEXT_DISCLOSURE
      System-level instruction shown exactly once whenever retrieved evidence is rendered.
      See Also:
  • Method Details

    • render

      public static String render(String userMessage, List<SearchResult> results)
      Renders search results for a single outbound LLM request.
      Parameters:
      userMessage - the original user message
      results - retrieved evidence; may be null or empty
      Returns:
      a fenced request, or the original message when no evidence can be rendered
    • render

      public static String render(String userMessage, RetrievalResult result, int contextWindow)
      Renders canonical retrieval evidence within the configured context window.
      Parameters:
      userMessage - original user message
      result - canonical retrieval result
      contextWindow - 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 message
      turn - the turn's DIRECT blocks
      result - SEARCH evidence, or null when nothing was searched
      contextWindow - 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 when result is rendered within contextWindow: the evidence that fits, as the render methods 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 it
      result - the search result, or null when nothing was searched
      contextWindow - maximum estimated tokens devoted to the evidence
      Returns:
      one SEARCH row per source
      Since:
      0.18.0
    • withUntrustedContextPolicy

      public static String withUntrustedContextPolicy(String systemPrompt)
      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

      public static String fence(String context)
      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