UNPKG

@langchain/langgraph-sdk

Version:

Client library for interacting with the LangGraph API

1 lines • 4.81 kB
{"version":3,"file":"queue.cjs","names":[],"sources":["../../src/ui/queue.ts"],"sourcesContent":["import type { SubmitOptions, CustomSubmitOptions } from \"./types.js\";\n\n/**\n * A single queued submission entry representing a server-side pending run.\n * Each entry corresponds to a run created on the server via\n * `client.runs.create()` with `multitaskStrategy: \"enqueue\"`.\n */\nexport interface QueueEntry<\n StateType extends Record<string, unknown> = Record<string, unknown>,\n OptionsType = SubmitOptions<StateType> | CustomSubmitOptions<StateType>,\n> {\n /** Server-side run ID (from `client.runs.create()`) */\n id: string;\n\n /** The state update values passed to submit() */\n values: Partial<StateType> | null | undefined;\n\n /** The submit options passed to submit() */\n options?: OptionsType;\n\n /** Timestamp when the entry was created */\n createdAt: Date;\n}\n\n/**\n * Reactive interface exposed to framework consumers for observing\n * and managing the server-side submission queue.\n */\nexport interface QueueInterface<\n StateType extends Record<string, unknown> = Record<string, unknown>,\n OptionsType = SubmitOptions<StateType> | CustomSubmitOptions<StateType>,\n> {\n /** Read-only array of pending queue entries */\n readonly entries: ReadonlyArray<QueueEntry<StateType, OptionsType>>;\n\n /** Number of pending entries */\n readonly size: number;\n\n /** Cancel a specific pending run by its server run ID. Returns true if found and cancelled. */\n cancel: (id: string) => Promise<boolean>;\n\n /** Cancel all pending runs on the server and clear the queue. */\n clear: () => Promise<void>;\n}\n\n/**\n * Tracks pending server-side runs created via `multitaskStrategy: \"enqueue\"`.\n *\n * Uses the same subscribe/getSnapshot pattern as StreamManager\n * to integrate with framework-specific reactivity systems.\n */\nexport class PendingRunsTracker<\n StateType extends Record<string, unknown> = Record<string, unknown>,\n OptionsType = SubmitOptions<StateType> | CustomSubmitOptions<StateType>,\n> {\n private pending: QueueEntry<StateType, OptionsType>[] = [];\n\n private listeners = new Set<() => void>();\n\n /**\n * Add a pending run entry.\n */\n add(entry: QueueEntry<StateType, OptionsType>): void {\n this.pending.push(entry);\n this.notifyListeners();\n }\n\n /**\n * Remove and return the next pending entry (FIFO).\n */\n shift(): QueueEntry<StateType, OptionsType> | undefined {\n const entry = this.pending.shift();\n if (entry) this.notifyListeners();\n return entry;\n }\n\n /**\n * Remove a specific entry by ID.\n * @returns true if the entry was found and removed.\n */\n remove = (id: string): boolean => {\n const index = this.pending.findIndex((e) => e.id === id);\n if (index === -1) return false;\n this.pending.splice(index, 1);\n this.notifyListeners();\n return true;\n };\n\n /**\n * Remove all entries from the tracker.\n * @returns The removed entries (for server-side cancellation).\n */\n removeAll = (): QueueEntry<StateType, OptionsType>[] => {\n if (this.pending.length === 0) return [];\n const entries = [...this.pending];\n this.pending = [];\n this.notifyListeners();\n return entries;\n };\n\n /** Read-only snapshot of all pending entries. */\n get entries(): ReadonlyArray<QueueEntry<StateType, OptionsType>> {\n return this.pending;\n }\n\n /** Number of pending entries. */\n get size(): number {\n return this.pending.length;\n }\n\n /** Subscribe to state changes. Returns an unsubscribe function. */\n subscribe = (listener: () => void): (() => void) => {\n this.listeners.add(listener);\n return () => {\n this.listeners.delete(listener);\n };\n };\n\n /** Snapshot token for useSyncExternalStore compatibility. */\n getSnapshot = (): number => {\n return this.pending.length;\n };\n\n private notifyListeners(): void {\n for (const listener of this.listeners) {\n listener();\n }\n }\n}\n"],"mappings":";;;;;;;AAmDA,IAAa,qBAAb,MAGE;CACA,UAAwD,EAAE;CAE1D,4BAAoB,IAAI,KAAiB;;;;CAKzC,IAAI,OAAiD;AACnD,OAAK,QAAQ,KAAK,MAAM;AACxB,OAAK,iBAAiB;;;;;CAMxB,QAAwD;EACtD,MAAM,QAAQ,KAAK,QAAQ,OAAO;AAClC,MAAI,MAAO,MAAK,iBAAiB;AACjC,SAAO;;;;;;CAOT,UAAU,OAAwB;EAChC,MAAM,QAAQ,KAAK,QAAQ,WAAW,MAAM,EAAE,OAAO,GAAG;AACxD,MAAI,UAAU,GAAI,QAAO;AACzB,OAAK,QAAQ,OAAO,OAAO,EAAE;AAC7B,OAAK,iBAAiB;AACtB,SAAO;;;;;;CAOT,kBAAwD;AACtD,MAAI,KAAK,QAAQ,WAAW,EAAG,QAAO,EAAE;EACxC,MAAM,UAAU,CAAC,GAAG,KAAK,QAAQ;AACjC,OAAK,UAAU,EAAE;AACjB,OAAK,iBAAiB;AACtB,SAAO;;;CAIT,IAAI,UAA6D;AAC/D,SAAO,KAAK;;;CAId,IAAI,OAAe;AACjB,SAAO,KAAK,QAAQ;;;CAItB,aAAa,aAAuC;AAClD,OAAK,UAAU,IAAI,SAAS;AAC5B,eAAa;AACX,QAAK,UAAU,OAAO,SAAS;;;;CAKnC,oBAA4B;AAC1B,SAAO,KAAK,QAAQ;;CAGtB,kBAAgC;AAC9B,OAAK,MAAM,YAAY,KAAK,UAC1B,WAAU"}