Skip to content
4 min read · 741 words

Class: Retrievable

Defined in: src/lib/classes/retrievable.ts:136

An immutable, validated retrieved record (RAG content) held by the agent.

Remarks

Peer of @nhtio/adk!Memory / Message / Thought / ToolCall. Carries an explicit trustTier that LLM batteries branch on to choose the rendering envelope. The retrieval middleware that produced the record is the only party that knows its provenance — batteries MUST NOT auto-classify or infer the tier from source.

Constructors

Constructor

ts
new Retrievable(raw: RawRetrievable): Retrievable;

Defined in: src/lib/classes/retrievable.ts:190

Parameters

ParameterTypeDescription
rawRawRetrievableThe raw retrievable input validated against rawRetrievableSchema.

Returns

Retrievable

Throws

@nhtio/adk/exceptions!E_INVALID_INITIAL_RETRIEVABLE_VALUE when raw does not satisfy the schema.

Properties

PropertyModifierTypeDefault valueDescriptionDefined in
contentreadonly| Tokenizable | SpooledArtifactundefinedThe retrieved content: a @nhtio/adk!Tokenizable (inline text) or a @nhtio/adk!SpooledArtifact (reader-backed, large text living in a consumer store). Use Retrievable.estimateTokens for budgeting and Retrievable.contentString to materialise the body at render time.src/lib/classes/retrievable.ts:163
createdAtreadonlyDateTimeundefinedWhen the source record was created.src/lib/classes/retrievable.ts:173
idreadonlystringundefinedStable unique identifier for this retrieved record.src/lib/classes/retrievable.ts:156
kindreadonlystring | undefinedundefinedOptional semantic label.src/lib/classes/retrievable.ts:169
scorereadonlynumber | undefinedundefinedOptional relevance / similarity score in [0, 1].src/lib/classes/retrievable.ts:171
sourcereadonlystring | undefinedundefinedOptional provenance string.src/lib/classes/retrievable.ts:167
trustTierreadonlyRetrievableTrustTierundefinedTrust tier declared by the retrieval middleware.src/lib/classes/retrievable.ts:165
updatedAtreadonlyDateTimeundefinedWhen the source record was last modified.src/lib/classes/retrievable.ts:175
schemastaticObjectSchema<RawRetrievable>rawRetrievableSchemaValidator schema that accepts a RawRetrievable object. Remarks Reusable fragment for any schema that needs to validate or nest a retrievable record.src/lib/classes/retrievable.ts:143

Methods

[ENCODE_METHOD]()

ts
ENCODE_METHOD: unknown;

Defined in: src/lib/classes/retrievable.ts:302

Serialise this Retrievable into an @nhtio/encoder snapshot.

Returns

unknown

A RawRetrievable-shaped snapshot.

Remarks

Emits a RawRetrievable-shaped object; content is the live @nhtio/adk!Tokenizable or @nhtio/adk!SpooledArtifact (the encoder recurses — a reader-backed artifact round-trips as a handle, throwing @nhtio/adk!E_READER_NOT_DESCRIBABLE if its reader cannot describe itself). Round-trips via Retrievable.[DECODE_METHOD], which re-validates through the constructor.


contentString()

ts
contentString(): Promise<string>;

Defined in: src/lib/classes/retrievable.ts:285

Returns the content body as a single string.

Returns

Promise<string>

The full content body as a string.

Remarks

For a @nhtio/adk!Tokenizable this is synchronous in effect (resolved immediately); for a @nhtio/adk!SpooledArtifact it reads the full body from the backing store via @nhtio/adk!SpooledArtifact.asString. Always returns a Promise so callers have one code path; render helpers await it at the point the trust-tier envelope is built.


estimateTokens()

ts
estimateTokens(encoding:
  | "gpt2"
  | "r50k_base"
  | "p50k_base"
  | "p50k_edit"
  | "cl100k_base"
  | "o200k_base"
  | "gemini"
  | "gemma"
  | "llama2"
| "claude"): number | Promise<number>;

Defined in: src/lib/classes/retrievable.ts:270

Estimates the token count of the content under encoding.

Parameters

ParameterTypeDescription
encoding| "gpt2" | "r50k_base" | "p50k_base" | "p50k_edit" | "cl100k_base" | "o200k_base" | "gemini" | "gemma" | "llama2" | "claude"The encoding identifier to use for counting.

Returns

number | Promise<number>

The estimated token count.

Remarks

Delegates to the content's own estimateTokens: synchronous for a @nhtio/adk!Tokenizable (returns number), asynchronous for a @nhtio/adk!SpooledArtifact (returns Promise<number>, reading the bytes from the backing store on demand). Both shapes satisfy the adapter's token-budget path, which already awaits estimates.

Note: the SpooledArtifact branch materialises the full decoded string transiently to count tokens — reader-backing keeps the body off the permanent heap, but does not eliminate the transient allocation at budgeting time.


[DECODE_METHOD]()

ts
static DECODE_METHOD: Retrievable;

Defined in: src/lib/classes/retrievable.ts:321

Reconstruct a Retrievable from a Retrievable.[ENCODE_METHOD] snapshot.

Parameters

ParameterTypeDescription
dataunknownThe snapshot produced by Retrievable.[ENCODE_METHOD].

Returns

Retrievable

A fully-validated Retrievable.


isRetrievable()

ts
static isRetrievable(value: unknown): value is Retrievable;

Defined in: src/lib/classes/retrievable.ts:151

Returns true if value is a Retrievable instance.

Parameters

ParameterType
valueunknown

Returns

value is Retrievable

Remarks

Uses @nhtio/adk!isInstanceOf for cross-realm safety.