Skip to main content

Utils Package Cheatsheet

Dense reference for Cognesy/Utils package capabilities. All examples show public API methods only.

Core Utilities

Json - JSON Processing

Notes:
  • Json::encode() and Json::fromArray() throw InvalidArgumentException on encoding failure.
  • Json::decode() throws JsonException when no default is provided and input is invalid; returns default otherwise.

Result - Monadic Error Handling

Notes:
  • Failure::errorMessage() converts any error to a string representation.

Option - Optional Value Type

Str - String Operations

Arrays - Array Utilities

Files - File System Operations

Data Structures

DataMap - Nested Data Access

ImmutableDataMap - Read-Only DataMap

CachedMap - Lazy-Loading Map

Context - Typed Service Container

Key - Typed Service Token

Layer - Context Composition

Container - DI Container

ArrayList - Immutable Indexed List

ArrayMap - Immutable Key-Value Map

ArraySet - Immutable Hash-Based Set

Deque - Double-Ended Queue

Buffer - FIFO Buffers

TagMap - Tagged Collection

TagQuery - Fluent Tag Querying

Caching & Lazy Loading

Cached - Lazy Value Container

Notes:
  • Cached is final and immutable. There is no fresh() or reset method.

Utilities

Uuid - ID Generation

Time - Clock Abstractions

Profiler - Performance Measurement

XML Processing

Xml - XML to Array/Object

XmlElement - XML DOM Node

SimpleXmlParser - SimpleXML-based Parser

XmlValidator - XML Validation

JSON Schema

JsonSchema - Schema Definition

ToolSchema - Function Tool Wrapper

CanProvideJsonSchema - Contract

CLI Utilities

Console - Terminal Output

Color - ANSI Color Constants

Text & Code Utilities

TextRepresentation - Convert Any Value to String

Tokenizer - Token Counting

The default is tiktoken with o200k_base, resolved by TokenizerResolver on first use. Pick another one without touching code via INSTRUCTOR_TOKENIZER:
auto falls back to the bundled tokenizer when the tiktoken vocabulary cannot be obtained; the explicit forms do not - getting a different tokenizer than the one you asked for would mean silently getting different counts. Counting goes through CanCountTokens; drivers that can also produce token IDs implement CanTokenizeText (encode(), encoding()). Prefer injecting the contract over calling the static facade.
TiktokenDriver is ~25x faster on long texts and uses a third of the memory, but it downloads its vocabulary on first use and caches it on disk (override the location with TIKTOKEN_CACHE_DIR or the $cacheDir argument). That download is why Gpt3TokenizerDriver is still shipped: it is the offline fallback. Token counts are only comparable within one encoding. The default o200k_base produces different numbers than the bundled r50k_base - generally fewer, since it covers modern text more tightly - so persisted token budgets are worth revisiting. tiktoken’s own r50k_base is token-for-token identical to the bundled tokenizer, and is the way to keep the old numbers with the new speed. Components that count tokens take an optional CanCountTokens, falling back to Tokenizer::default(): SplitMessages, MoveMessagesToBuffer, SummarizeBuffer, and the agents UseSummarization capability. That fallback resolves on the first count, not in the constructor - wiring one of these components into a pipeline costs nothing until it actually counts. A consequence worth knowing: a component built without an explicit tokenizer picks up whatever Tokenizer::setDefault() installed most recently, so set the default before counting starts rather than assuming construction order decides it.

ProgrammingLanguage - Language Enum & Helpers

Data Helpers

Metadata - Immutable Key-Value Store

OpaqueExternalId - Abstract Typed Identifier

AbstractResolver - Priority-Based Provider Chain

Markdown

FrontMatter - YAML Front Matter Parser

Specialized Parsers

JsonExtractor - JSON Extraction from Text

JsonDecoder - Resilient JSON Decoder

IncrementalJsonParser - Streaming Chunk Parser

XML Parsers

All classes follow immutable patterns where applicable and use strict typing throughout.