Skip to main content
GitHub

JavaScript SDK

Complete JavaScript/TypeScript SDK reference.

Complete reference for the Risicare JavaScript/TypeScript SDK (v0.1.1).

Installation

npm install risicare

Core Functions

init

Initialize the SDK.

import { init } from 'risicare';
 
init(config?: RisicareConfig): void

enable / disable / isEnabled

Runtime control.

import { enable, disable, isEnabled } from 'risicare';
 
enable(): void
disable(): void
isEnabled(): boolean

flush / shutdown

Export control.

import { flush, shutdown } from 'risicare';
 
flush(): Promise<void>
shutdown(): Promise<void>

getMetrics

Returns processor metrics for monitoring export health.

import { getMetrics } from 'risicare';
 
getMetrics(): {
  exportedSpans: number;
  droppedSpans: number;
  failedExports: number;
  queueSize: number;
  queueCapacity: number;
  queueUtilization: number;
}

getTraceContent

Returns whether content tracing is enabled.

import { getTraceContent } from 'risicare';
 
getTraceContent(): boolean

Provider Patches

Provider patches are imported from sub-path exports:

patchOpenAI

import { patchOpenAI } from 'risicare/openai';
import OpenAI from 'openai';
 
patchOpenAI(client: OpenAI): OpenAI

patchAnthropic

import { patchAnthropic } from 'risicare/anthropic';
import Anthropic from '@anthropic-ai/sdk';
 
patchAnthropic(client: Anthropic): Anthropic

patchVercelAI

Returns higher-order wrapper functions for Vercel AI SDK functions:

import { patchVercelAI } from 'risicare/vercel-ai';
 
patchVercelAI(): {
  tracedGenerateText: <T extends Function>(fn: T) => T;
  tracedStreamText: <T extends Function>(fn: T) => T;
  tracedGenerateObject: <T extends Function>(fn: T) => T;
}

Usage:

import { generateText, streamText, generateObject } from 'ai';
import { patchVercelAI } from 'risicare/vercel-ai';
 
const { tracedGenerateText, tracedStreamText, tracedGenerateObject } = patchVercelAI();
const generate = tracedGenerateText(generateText);
const stream = tracedStreamText(streamText);
const genObject = tracedGenerateObject(generateObject);

Higher-Order Functions

agent

import { agent } from 'risicare';
 
agent<T extends (...args: any[]) => any>(
  options: {
    name?: string;
    role?: 'orchestrator' | 'worker' | 'reviewer';
    agentType?: string;
    version?: number;
  },
  fn: T
): T

session

import { session } from 'risicare';
 
session<T extends (...args: any[]) => any>(
  optionsOrResolver: SessionOptions | ((...args: any[]) => SessionOptions),
  fn: T
): T
 
interface SessionOptions {
  sessionId: string;
  userId?: string;
}

Phase Functions

import { traceThink, traceDecide, traceAct, traceObserve } from 'risicare';
 
traceThink<T extends (...args: any[]) => any>(fn: T): T
traceDecide<T extends (...args: any[]) => any>(fn: T): T
traceAct<T extends (...args: any[]) => any>(fn: T): T
traceObserve<T extends (...args: any[]) => any>(fn: T): T

Multi-Agent Functions

import { traceMessage, traceDelegate, traceCoordinate } from 'risicare';
 
traceMessage<T extends (...args: any[]) => any>(
  options: { to: string },
  fn: T
): T
 
traceDelegate<T extends (...args: any[]) => any>(
  options: { to: string },
  fn: T
): T
 
traceCoordinate<T extends (...args: any[]) => any>(
  options: { participants: string[] },
  fn: T
): T

Context Functions

getCurrentContext

import { getCurrentContext } from 'risicare';
 
getCurrentContext(): ContextState | undefined
 
interface ContextState {
  session?: { sessionId: string; userId?: string };
  agent?: { name: string; role?: string; agentId?: string };
  span?: Span;
  phase?: SemanticPhase;
}

Individual Getters

import {
  getCurrentTraceId,
  getCurrentSpanId,
  getCurrentAgentId,
  getCurrentSessionId,
} from 'risicare';
 
getCurrentTraceId(): string | undefined
getCurrentSpanId(): string | undefined
getCurrentAgentId(): string | undefined
getCurrentSessionId(): string | undefined

W3C Trace Context

import { injectTraceContext, extractTraceContext } from 'risicare';
 
injectTraceContext(headers: Record<string, string>): void
extractTraceContext(headers: Record<string, string>): TraceContext | undefined

Span Registry

import {
  registerSpan,
  getSpanById,
  unregisterSpan,
} from 'risicare';
 
registerSpan(span: Span, ttlMs?: number): void
getSpanById(spanId: string): Span | undefined
unregisterSpan(spanId: string): void

Types

RisicareConfig

interface RisicareConfig {
  apiKey?: string;
  projectId?: string;
  endpoint?: string;
  environment?: string;
  serviceName?: string;
  serviceVersion?: string;
  enabled?: boolean;
  traceContent?: boolean;
  compress?: boolean;
  sampleRate?: number;
  batchSize?: number;
  batchTimeoutMs?: number;
  maxQueueSize?: number;
  debug?: boolean;
  metadata?: Record<string, unknown>;
}

TraceContext

interface TraceContext {
  traceId: string;
  spanId: string;
  sessionId?: string;
  agentId?: string;
}

Span

class Span {
  traceId: string;
  spanId: string;
  name: string;
  kind: SpanKind;
  startTime: number;
  endTime?: number;
  attributes: Record<string, unknown>;
  events: SpanEvent[];
  status: SpanStatus;
 
  setAttribute(key: string, value: unknown): this;
  addEvent(name: string, attributes?: Record<string, unknown>): this;
  setStatus(status: SpanStatus): this;
  end(): void;
}

SpanKind

enum SpanKind {
  INTERNAL = 'internal',
  SERVER = 'server',
  CLIENT = 'client',
  PRODUCER = 'producer',
  CONSUMER = 'consumer',
  AGENT = 'agent',
  LLM_CALL = 'llm_call',
  TOOL_CALL = 'tool_call',
  RETRIEVAL = 'retrieval',
  DECISION = 'decision',
  DELEGATION = 'delegation',
  COORDINATION = 'coordination',
  MESSAGE = 'message',
  THINK = 'think',
  DECIDE = 'decide',
  OBSERVE = 'observe',
  REFLECT = 'reflect',
}

SpanStatus

enum SpanStatus {
  UNSET = 'unset',
  OK = 'ok',
  ERROR = 'error',
}

Next Steps