@langchain/core
Version:
Core LangChain.js abstractions and schemas
185 lines (184 loc) • 5.76 kB
text/typescript
import { ContentBlock } from "../messages/content/index.cjs";
import { UsageMetadata } from "../messages/metadata.cjs";
//#region src/language_models/event.d.ts
/**
* Emitted once at the start of a model response.
*/
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.
*/
type FinishReason = "stop" | "length" | "tool_use" | "content_filter";
/**
* Emitted once when the model response is complete.
*/
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: "" } }
* ```
*/
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.
*/
interface TextDelta {
type: "text-delta";
/** The new text to append. */
text: string;
}
/**
* Incremental reasoning content. Append `reasoning` to the active block's
* `reasoning` field.
*/
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.
*/
interface DataDelta {
type: "data-delta";
/** Encoded data chunk to append. */
data: string;
/** Encoding for the data chunk. Defaults to `"base64"` when omitted. */
encoding?: "base64";
}
/**
* Generic content block field update. Shallow-merge `fields` onto the active
* content block.
*/
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.
*/
type ContentBlockDelta = TextDelta | ReasoningDelta | DataDelta | 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
* - `block-delta` → shallow-merge `fields` onto the active block
*
* @example
* ```ts
* // Text token
* { event: "content-block-delta", index: 0,
* delta: { type: "text-delta", text: " world" } }
*
* // 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" } } }
* ```
*/
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" } }
* ```
*/
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).
*/
interface UsageUpdateEvent {
event: "usage";
/** Current usage snapshot. */
usage: Partial<UsageMetadata>;
}
/**
* Passthrough for native provider events that don't map to standard types.
*/
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.
*/
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.
*/
type ChatModelStreamEvent = MessageStartEvent | MessageFinishEvent | ContentBlockStartEvent | ContentBlockDeltaEvent | ContentBlockFinishEvent | UsageUpdateEvent | ProviderEvent | StreamErrorEvent;
//#endregion
export { BlockDelta, ChatModelStreamEvent, ContentBlockDelta, ContentBlockDeltaEvent, ContentBlockFinishEvent, ContentBlockStartEvent, DataDelta, FinishReason, MessageFinishEvent, MessageStartEvent, ProviderEvent, ReasoningDelta, StreamErrorEvent, TextDelta, UsageUpdateEvent };
//# sourceMappingURL=event.d.cts.map