IntentLayerDocsDashboard ↗
Docs/SDKs
SDKs

TypeScript SDK reference

Public core, OpenAI adapter, and Vercel AI middleware exports with exact options and defaults.

Package exports root client plus intentlayer/openai and intentlayer/vercel-ai subpaths.

#Core types

Mode is "observe" | "suggest" | "auto".

Outcome is "rephrase" | "accept" | "regenerate" | "abandon" | "correction".

typescript
import type {
  GateResult,
  IntentLayerOptions,
  IntentSpec,
  Mode,
  Outcome,
  RecordOutcomeOptions,
} from "intentlayer";

IntentSpec fields are goal, outputFormat, scope, constraints, and missingFields. GateResult fields are ambiguityScore, dimensions, questions, and intentSpec.

IntentLayerOptions:

FieldTypeDefault
apiKeystringINTENTLAYER_API_KEY or empty
baseUrlstringINTENTLAYER_BASE_URL or http://localhost:8000
modeModeINTENTLAYER_MODE or suggest
timeoutMsnumber8000

RecordOutcomeOptions has optional gateCaught.

#IntentLayer

typescript
import { IntentLayer } from "intentlayer";

const il = new IntentLayer(options?: IntentLayerOptions);
await il.gate(
  request: string,
  sessionId: string,
  context?: Record<string, unknown> | null,
): Promise<GateResult>;

il.recordOutcome(
  sessionId: string,
  event: Outcome,
  detail?: string,
  options?: RecordOutcomeOptions,
): void;

const wrapped = il.wrap(agent);

mode is public readonly. gate posts snake_case wire fields and maps response to camelCase. recordOutcome is fire-and-forget. wrap returns async function, awaits gate, then awaits agent.

#OpenAI adapter

typescript
import {
  isIntentLayerSuggestion,
  wrapOpenAI,
  type IntentLayerSuggestion,
  type OpenAIWrapperOptions,
  type WrappedOpenAIClient,
} from "intentlayer/openai";

OpenAIWrapperOptions requires sessionId, string or function receiving request and extracted user message. Optional interceptSuggestions defaults false. Optional ambiguityThreshold defaults 0.55.

wrapOpenAI(client, intentLayer, options) requires client.chat.completions.create. It gates last user text. In suggest mode with interception true, score at threshold, and nonempty questions, it returns IntentLayerSuggestion; otherwise original create runs.

isIntentLayerSuggestion(result) is type guard checking object marker intentlayer.questions.

#Vercel AI adapter

typescript
import {
  intentLayerMiddleware,
  type VercelAIWrapperOptions,
} from "intentlayer/vercel-ai";

VercelAIWrapperOptions.sessionId is string or function receiving LanguageModelV3CallOptions. intentLayerMiddleware(intentLayer, options) returns LanguageModelV3Middleware with specification version v3.

Generate and stream wrappers gate last user turn, call underlying model, then record accept after generate/stream setup succeeds. Results and streams pass through unchanged.

Source truthsdk-js/src/index.tssdk-js/src/openai.tssdk-js/src/vercel-ai.tssdk-js/package.json