Skip to main content
GitHub

JS Decorators

Higher-order functions for adding observability context in JavaScript/TypeScript.

Risicare provides higher-order functions that wrap your functions with observability context. These work similarly to Python decorators.

Agent Functions

agent()

Wrap a function as an agent entry point:

import { agent } from 'risicare';
 
const myAgent = agent(
  {
    name: 'researcher',
    role: 'worker',
    agentType: 'search',
    version: 1,
  },
  async (query: string) => {
    // All spans here are associated with this agent
    return await processQuery(query);
  }
);
 
// Use the wrapped function
const result = await myAgent('What is the weather?');

Options (first parameter):

OptionTypeDefaultDescription
namestringFunction nameAgent name
rolestringundefinedAgent role (orchestrator, worker, reviewer)
agentTypestringundefinedType identifier
versionnumberundefinedAgent version

Session Functions

session()

Bind a function to a user session:

import { session } from 'risicare';
 
const handleRequest = session(
  {
    sessionId: 'sess-123',
    userId: 'user-456',
  },
  async (query: string) => {
    // All traces grouped under this session
    return await processRequest(query);
  }
);

You can also use a resolver function for dynamic sessions:

const handleRequest = session(
  (sessionId: string, userId: string) => ({
    sessionId,
    userId,
  }),
  async (sessionId: string, userId: string, query: string) => {
    return await processRequest(query);
  }
);

SessionOptions:

OptionTypeRequiredDescription
sessionIdstringYesSession identifier
userIdstringNoUser identifier

Phase Functions

Track semantic decision phases:

traceThink()

import { traceThink } from 'risicare';
 
const analyze = traceThink(async (context: Context) => {
  // Reasoning logic
  return analysis;
});

traceDecide()

import { traceDecide } from 'risicare';
 
const plan = traceDecide(async (options: Option[]) => {
  // Decision logic
  return selectedOption;
});

traceAct()

import { traceAct } from 'risicare';
 
const execute = traceAct(async (action: Action) => {
  // Action execution
  return result;
});

traceObserve()

import { traceObserve } from 'risicare';
 
const checkState = traceObserve(async (key: string) => {
  // State reading
  return memory.get(key);
});

Multi-Agent Functions

traceMessage()

Track inter-agent message passing:

import { traceMessage } from 'risicare';
 
const sendMessage = traceMessage(
  { to: 'reviewer-agent' },
  async (message: string) => {
    return await router.send('reviewer-agent', message);
  }
);

traceDelegate()

Track task delegation:

import { traceDelegate } from 'risicare';
 
const delegateTask = traceDelegate(
  { to: 'worker-agent' },
  async (task: Task) => {
    return await workers.get('worker-agent').execute(task);
  }
);

traceCoordinate()

Track multi-agent coordination:

import { traceCoordinate } from 'risicare';
 
const syncAgents = traceCoordinate(
  { participants: ['agent-a', 'agent-b', 'agent-c'] },
  async () => {
    return await coordinator.sync(['agent-a', 'agent-b', 'agent-c']);
  }
);

Complete Multi-Agent Example

import { agent, traceThink, traceDecide, traceAct, traceDelegate } from 'risicare';
 
// Define sub-agents
const researcher = agent(
  { name: 'researcher', role: 'worker' },
  async (query: string) => {
    const analyze = traceThink(async () => parseQuery(query));
    const search = traceAct(async () => searchDatabase(query));
 
    await analyze();
    return await search();
  }
);
 
// Define orchestrator
const orchestrator = agent(
  { name: 'orchestrator', role: 'orchestrator' },
  async (task: string) => {
    const plan = traceDecide(async () => createPlan(task));
    const delegate = traceDelegate(
      { to: 'researcher' },
      async () => researcher(task)
    );
 
    await plan();
    return await delegate();
  }
);
 
// Run
const result = await orchestrator('Research AI trends');

Next Steps