@langchain/langgraph-sdk
Version:
Client library for interacting with the LangGraph API
235 lines (234 loc) • 7.22 kB
text/typescript
import { Checkpoint, Config, DefaultValues, Metadata, SortOrder, Thread, ThreadSelectField, ThreadSortBy, ThreadState, ThreadStatus, ThreadValuesFilter } from "../../schema.cjs";
import { ThreadStreamMode } from "../../types.stream.cjs";
import { Command, OnConflictBehavior, StreamEvent } from "../../types.cjs";
import { BaseClient } from "../base.cjs";
import { ThreadStreamOptions } from "../stream/types.cjs";
import { ThreadStream } from "../stream/index.cjs";
//#region src/client/threads/index.d.ts
declare class ThreadsClient<TStateType = DefaultValues, TUpdateType = TStateType> extends BaseClient {
/**
* Get a thread by ID.
*
* @param threadId ID of the thread.
* @returns The thread.
*/
get<ValuesType = TStateType>(threadId: string, options?: {
signal?: AbortSignal;
include?: string[];
}): Promise<Thread<ValuesType>>;
/**
* Create a new thread.
*
* @param payload Payload for creating a thread.
* @returns The created thread.
*/
create(payload?: {
metadata?: Metadata;
threadId?: string;
ifExists?: OnConflictBehavior;
graphId?: string;
supersteps?: Array<{
updates: Array<{
values: unknown;
command?: Command;
asNode: string;
}>;
}>;
ttl?: number | {
ttl: number;
strategy?: "delete";
};
signal?: AbortSignal;
}): Promise<Thread<TStateType>>;
/**
* Copy an existing thread
* @param threadId ID of the thread to be copied
* @returns Newly copied thread
*/
copy(threadId: string, options?: {
signal?: AbortSignal;
}): Promise<Thread<TStateType>>;
/**
* Update a thread.
*
* @param threadId ID of the thread.
* @param payload Payload for updating the thread.
* @returns The updated thread.
*/
update(threadId: string, payload?: {
metadata?: Metadata;
ttl?: number | {
ttl: number;
strategy?: "delete";
};
returnMinimal?: false;
signal?: AbortSignal;
}): Promise<Thread>;
update(threadId: string, payload: {
metadata?: Metadata;
ttl?: number | {
ttl: number;
strategy?: "delete";
};
returnMinimal: true;
signal?: AbortSignal;
}): Promise<void>;
update(threadId: string, payload: {
metadata?: Metadata;
ttl?: number | {
ttl: number;
strategy?: "delete";
};
returnMinimal: boolean;
signal?: AbortSignal;
}): Promise<Thread | void>;
/**
* Delete a thread.
*
* @param threadId ID of the thread.
*/
delete(threadId: string, options?: {
signal?: AbortSignal;
}): Promise<void>;
/**
* Prune threads by ID. The 'delete' strategy removes threads entirely.
* The 'keep_latest' strategy prunes old checkpoints but keeps threads
* and their latest state.
*
* @param threadIds List of thread IDs to prune.
* @param options Additional options for pruning.
* @param options.strategy The prune strategy. Defaults to 'delete'.
* @param options.signal Signal to abort the request.
* @returns An object containing `pruned_count`.
*/
prune(threadIds: string[], options?: {
strategy?: "delete" | "keep_latest";
signal?: AbortSignal;
}): Promise<{
pruned_count: number;
}>;
/**
* List threads
*
* @param query Query options
* @returns List of threads
*/
search<ValuesType = TStateType>(query?: {
metadata?: Metadata;
ids?: string[];
limit?: number;
offset?: number;
status?: ThreadStatus;
sortBy?: ThreadSortBy;
sortOrder?: SortOrder;
select?: ThreadSelectField[];
values?: ThreadValuesFilter;
extract?: Record<string, string>;
signal?: AbortSignal;
}): Promise<Thread<ValuesType>[]>;
/**
* Count threads matching filters.
*
* @param query.metadata Thread metadata to filter on.
* @param query.values State values to filter on.
* @param query.status Thread status to filter on.
* @returns Number of threads matching the criteria.
*/
count<ValuesType = TStateType>(query?: {
metadata?: Metadata;
values?: ValuesType;
status?: ThreadStatus;
signal?: AbortSignal;
}): Promise<number>;
/**
* Get state for a thread.
*
* @param threadId ID of the thread.
* @returns Thread state.
*/
getState<ValuesType = TStateType>(threadId: string, checkpoint?: Checkpoint | string, options?: {
subgraphs?: boolean;
signal?: AbortSignal;
}): Promise<ThreadState<ValuesType>>;
/**
* Add state to a thread.
*
* @param threadId The ID of the thread.
* @returns
*/
updateState<ValuesType = TUpdateType>(threadId: string, options: {
values: ValuesType;
checkpoint?: Checkpoint;
checkpointId?: string;
asNode?: string;
signal?: AbortSignal;
}): Promise<Pick<Config, "configurable">>;
/**
* Patch the metadata of a thread.
*
* @param threadIdOrConfig Thread ID or config to patch the state of.
* @param metadata Metadata to patch the state with.
*/
patchState(threadIdOrConfig: string | Config, metadata: Metadata, options?: {
signal?: AbortSignal;
}): Promise<void>;
/**
* Get all past states for a thread.
*
* @param threadId ID of the thread.
* @param options Additional options.
* @returns List of thread states.
*/
getHistory<ValuesType = TStateType>(threadId: string, options?: {
limit?: number;
before?: Config;
checkpoint?: Partial<Omit<Checkpoint, "thread_id">>;
metadata?: Metadata;
signal?: AbortSignal;
}): Promise<ThreadState<ValuesType>[]>;
joinStream(threadId: string, options?: {
lastEventId?: string;
streamMode?: ThreadStreamMode | ThreadStreamMode[];
signal?: AbortSignal;
}): AsyncGenerator<{
id?: string;
event: StreamEvent;
data: any;
}>;
/**
* Open a protocol stream over the thread-centric v2 protocol.
*
* Returns a {@link ThreadStream} with lazy getters
* (`.messages`, `.values`, `.toolCalls`, `.subgraphs`, `.subagents`,
* `.output`) and `thread.run.start({ input, ... })` for starting runs.
* Mirrors the in-process `graph.streamEvents(..., { version: "v3" })` API.
*
* The thread is bound to `options.assistantId` for its lifetime.
* The wire transport defaults to SSE; pass `transport: "websocket"`
* in options (or configure `streamProtocol: "v2-websocket"` on the
* client) to use a WebSocket instead.
*
* @example New thread (UUID generated client-side)
* ```ts
* const thread = client.threads.stream({ assistantId: "my-agent" });
* ```
*
* @example Attach to an existing thread
* ```ts
* const thread = client.threads.stream(threadId, { assistantId: "my-agent" });
* ```
*
* @example WebSocket transport
* ```ts
* const thread = client.threads.stream({
* assistantId: "my-agent",
* transport: "websocket",
* });
* ```
*/
stream<TExtensions extends Record<string, unknown> = Record<string, unknown>>(options: ThreadStreamOptions): ThreadStream<TExtensions>;
stream<TExtensions extends Record<string, unknown> = Record<string, unknown>>(threadId: string, options: ThreadStreamOptions): ThreadStream<TExtensions>;
}
//#endregion
export { ThreadsClient };
//# sourceMappingURL=index.d.cts.map