@kognitivedev/shared

v0.2.8

Published

Foundation utilities for the Kognitive AI framework

Readme

@kognitivedev/shared

Foundation utilities for the Kognitive AI framework — EventBus, Cache, Logger, Errors, Retry, and OpenTelemetry integration.

Installation

bun add @kognitivedev/shared

Exports

Events

  • InMemoryEventBus — typed pub/sub: emit(), on(), once(), removeAll()

Cache

  • InMemoryCacheAdapter — TTL-based cache: get(), set(), delete(), clear()

Logger

  • ConsoleLogger — structured logging with prefix
  • NoopLogger — silent logger for testing

Errors

  • KognitiveError, RetryableError, ValidationError, TimeoutError, NotFoundError, ApprovalDeniedError

Utilities

  • withRetry(fn, options) — exponential backoff retry
  • generateId() — UUID generation

Telemetry

  • withSpan(name, fn, attributes?) — OpenTelemetry span wrapper (no-op if OTEL not installed)
  • createSpan(), endSpan() — manual span control

Streaming

  • StreamMode"values" | "updates" | "messages" | "custom" | "debug"
  • StreamEvent{ event: string; data: unknown }
  • parseStreamModes(input) — validate and parse comma-separated mode string
  • createSSEEncoder()TransformStream<StreamEvent, Uint8Array> for SSE wire format
  • sseHeaders() — standard SSE response headers

Types

  • ResourceId{ organizationId?, projectId, userId?, sessionId? }
  • Result<T, E> — discriminated union with ok() / err() helpers