@langchain/core
Version:
Core LangChain.js abstractions and schemas
206 lines • 6.55 kB
TypeScript
import { ContentBlock } from "../messages/content/index.js";
import { UsageMetadata } from "../messages/metadata.js";
/**
* Emitted once at the start of a model response.
*/
export interface MessageStartEvent {
event: "message-start";
/** Optional message ID assigned by the provider. */
id?: string;
/**
* Initial usage snapshot, if the provider reports input token counts
* before content begins streaming (e.g., Anthropic's `message_start`).
*/
usage?: Partial<UsageMetadata>;
}
/**
* Finish reason for a model response.
*
* - `"stop"`: Natural end of generation.
* - `"length"`: Hit max token limit.
* - `"tool_use"`: Model is requesting tool execution.
* - `"content_filter"`: Content was filtered by safety systems.
*/
export type FinishReason = "stop" | "length" | "tool_use" | "content_filter";
/**
* Emitted once when the model response is complete.
*/
export interface MessageFinishEvent {
event: "message-finish";
/** Why the model stopped generating. */
reason?: FinishReason;
/** Final usage snapshot. */
usage?: Partial<UsageMetadata>;
/** Provider-specific response metadata (model name, response ID, headers, etc.). */
responseMetadata?: Record<string, unknown>;
}
/**
* Emitted when a new content block begins streaming.
*
* @example
* ```ts
* { event: "content-block-start", index: 0,
* content: { type: "text", text: "" } }
* ```
*/
export interface ContentBlockStartEvent {
event: "content-block-start";
/** Positional index of this block within the message. */
index: number;
/** Initial state of the content block. */
content: ContentBlock;
}
/**
* Incremental text content. Append `text` to the active block's `text` field.
*/
export interface TextDelta {
type: "text-delta";
/** The new text to append. */
text: string;
}
/**
* Incremental reasoning content. Append `reasoning` to the active block's
* `reasoning` field.
*/
export interface ReasoningDelta {
type: "reasoning-delta";
/** The new reasoning text to append. */
reasoning: string;
}
/**
* Incremental encoded data. Append `data` to the active multimodal block's
* data field.
*/
export interface DataDelta {
type: "data-delta";
/** Encoded data chunk to append. */
data: string;
/** Encoding for the data chunk. Defaults to `"base64"` when omitted. */
encoding?: "base64";
}
/**
* Incremental tool-call arguments. Append `args` to the active
* `tool_call_chunk` or `server_tool_call_chunk` block's `args` field, treating
* missing args as an empty string.
*
* A slice need not be valid JSON on its own. The call's `id` and `name` arrive
* on `content-block-start`, and `content-block-finish` carries the parsed call.
*/
export interface ToolCallDelta {
type: "tool-call-delta";
/** The new slice of the call's arguments string to append. */
args: string;
}
/**
* Generic content block field update. Shallow-merge `fields` onto the active
* content block.
*/
export interface BlockDelta {
type: "block-delta";
/** Fields to shallow-merge onto the active content block. */
fields: {
type: string;
} & Record<string, unknown>;
}
/**
* Union of all content block delta types.
*/
export type ContentBlockDelta = TextDelta | ReasoningDelta | DataDelta | ToolCallDelta | BlockDelta;
/**
* Emitted for each incremental update within a content block.
*
* The `delta` field carries the incremental content block update.
* Accumulation rules:
* - `text-delta` → append `text` to the active block's text field
* - `reasoning-delta` → append `reasoning` to the active block's reasoning field
* - `data-delta` → append `data` to the active block's data field
* - `tool-call-delta` → append `args` to the active tool-call block's args field
* - `block-delta` → shallow-merge `fields` onto the active block
*
* Consumers ignore delta types they don't recognize.
* `content-block-finish` carries the complete block.
*
* A tool call's args stream in one of two forms, never both in one block:
* `tool-call-delta` slices, which append, or `block-delta` snapshots of the
* args so far, which replace.
*
* @example
* ```ts
* // Text token
* { event: "content-block-delta", index: 0,
* delta: { type: "text-delta", text: " world" } }
*
* // Tool call args slice
* { event: "content-block-delta", index: 1,
* delta: { type: "tool-call-delta", args: '"wea' } }
*
* // Tool call args snapshot
* { event: "content-block-delta", index: 1,
* delta: { type: "block-delta",
* fields: { type: "tool_call_chunk", args: '{"q":"wea' } } }
*
* // Provider-specific field (e.g., signature)
* { event: "content-block-delta", index: 0,
* delta: { type: "block-delta",
* fields: { type: "reasoning", signature: "sig_abc" } } }
* ```
*/
export interface ContentBlockDeltaEvent {
event: "content-block-delta";
/** Positional index of the block being updated. */
index: number;
/** Incremental content block delta. */
delta: ContentBlockDelta;
}
/**
* Emitted when a content block is complete.
*
* @example
* ```ts
* { event: "content-block-finish", index: 0,
* content: { type: "text", text: "Hello world" } }
* ```
*/
export interface ContentBlockFinishEvent {
event: "content-block-finish";
/** Positional index of the completed block. */
index: number;
/** Finalized content block. */
content: ContentBlock;
}
/**
* Emitted whenever the provider reports updated usage information.
* Each event carries a **running snapshot** (not an additive delta).
*/
export interface UsageUpdateEvent {
event: "usage";
/** Current usage snapshot. */
usage: Partial<UsageMetadata>;
}
/**
* Passthrough for native provider events that don't map to standard types.
*/
export interface ProviderEvent {
event: "provider";
/** Provider identifier (e.g., `"openai"`, `"anthropic"`, `"google"`). */
provider: string;
/** Raw event type name from the provider SDK. */
name: string;
/** Raw event payload from the provider SDK. */
payload: unknown;
}
/**
* Emitted on unrecoverable stream errors.
*/
export interface StreamErrorEvent {
event: "error";
/** Human-readable error message. */
message: string;
/** Optional error code for programmatic handling. */
code?: string;
}
/**
* Union of all chat model stream event types.
*/
export type ChatModelStreamEvent = MessageStartEvent | MessageFinishEvent | ContentBlockStartEvent | ContentBlockDeltaEvent | ContentBlockFinishEvent | UsageUpdateEvent | ProviderEvent | StreamErrorEvent;
//# sourceMappingURL=event.d.ts.map