/** * Worker-side LangSmith emission, exposed as a Temporal {@link InjectedSinks}. * * @module */ import type { Client } from 'langsmith'; import { RunTree, type RunTreeConfig, type WriteReplica } from 'langsmith/run_trees'; import type { Logger } from '@temporalio/common'; import type { Sinks } from '@temporalio/workflow'; import type { InjectedSinks } from '@temporalio/worker'; import { bindReplicaCredentials, type ReplicaRouting } from './routing'; /** Plain-JSON description of a LangSmith run crossing the isolate→worker boundary. */ export interface SerializedRun { id: string; trace_id: string; dotted_order: string; parent_run_id?: string; name: string; run_type: string; start_time: number; /** End time (epoch ms); present only on the update record. */ end_time?: number; inputs?: Record; outputs?: Record; /** Error string; non-null marks the run errored. */ error?: string; extra?: Record; tags?: string[]; project_name?: string; address?: RunTreeConfig['address']; replicas?: ReplicaRouting[]; distributedParentId?: string; events?: Record[]; } /** Runtime configuration shared by the client and activity interceptors. */ export interface EmitterConfig { /** The LangSmith client runs are emitted to. */ client: Client; /** When false, no Temporal-operation runs are emitted (propagation only). */ addTemporalRuns: boolean; /** Target LangSmith project. */ projectName?: string; address?: RunTreeConfig['address']; /** Full replica configuration is local to the client/worker process. */ replicas?: WriteReplica[]; /** Tags attached to every emitted run. */ tags?: string[]; /** Metadata merged into every emitted run. */ metadata?: Record; } /** The injected sink's wire name. Reserved-prefixed; allowlisted in `@temporalio/common`. */ export const LANGSMITH_SINK_NAME = '__temporal_langsmith' as const; /** The Temporal sink surface this plugin injects. */ export interface LangSmithSinks extends Sinks { [LANGSMITH_SINK_NAME]: { /** Emit a `createRun` for a newly started run. */ createRun(run: SerializedRun): void; /** Emit an `updateRun` (end / outputs / error / events) for a run. */ updateRun(run: SerializedRun, options?: Parameters[0]): void; }; } function emissionRun(run: SerializedRun, client: Client, replicas: WriteReplica[] | undefined): RunTree { const tree = new RunTree({ ...run, inputs: run.inputs ?? {}, client, replicas: bindReplicaCredentials(run.replicas, replicas), tracingEnabled: true, }); tree.events = run.events as RunTree['events']; return tree; } /** * Build the worker-side {@link InjectedSinks} routing serialized runs to a real LangSmith client, and a `drain` that * resolves once every queued write has reached the client. */ export function createLangSmithSinks( client: Client, replicas: WriteReplica[] | undefined, logger: Logger ): { sinks: InjectedSinks; drain: () => Promise } { // Writes run one at a time in call order, so an update never overtakes its create. let writes = Promise.resolve(); // The Workflow already decided whether to trace each run, so the env isn't checked again here. const enqueue = (write: () => Promise): void => { // Emission must never fail the workflow. writes = writes.then(write).catch((err: unknown) => { logger.error(`LangSmith background I/O error: ${String(err)}`); }); }; return { sinks: { [LANGSMITH_SINK_NAME]: { createRun: { fn: (_info, run) => enqueue(() => emissionRun(run, client, replicas).postRun()), callDuringReplay: false, }, updateRun: { fn: (_info, run, options) => enqueue(() => emissionRun(run, client, replicas).patchRun(options)), callDuringReplay: false, }, }, }, drain: () => writes, }; }