UNPKG

@openai/agents-core

Version:

The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.

109 lines (108 loc) • 4.01 kB
import { Trace, TraceOptions } from './traces'; import { Span, SpanError } from './spans'; type ContextState = { trace?: Trace; span?: Span<any>; previousSpan?: Span<any>; active: boolean; }; export type TraceContextSnapshot = Readonly<{ trace: Trace; span?: Span<any> | null; }>; export type TracingContextStorage<TStore = any> = { /** * Runs the callback with the provided store as the active tracing context. */ run<TResult>(store: TStore, callback: () => TResult): TResult; /** * Returns the active tracing context store, if one exists. */ getStore(): TStore | undefined; /** * Replaces the active tracing context store. The store is undefined when * tracing clears the current scope. */ enterWith(store: TStore | undefined): void; }; /** * Sets the context storage implementation used for tracing. * * Use this before starting traces in runtimes that cannot rely on the SDK's default * AsyncLocalStorage implementation. Pass undefined to restore the default storage. */ export declare function setTracingContextStorage(storage?: TracingContextStorage): void; /** * This function will get the current trace from the execution context. * * @returns The current trace or null if there is no trace. */ export declare function getCurrentTrace(): Trace | null; /** * This function will get the current span from the execution context. * * @returns The current span or null if there is no span. */ export declare function getCurrentSpan(): Span<any> | null; /** * Gets a public snapshot of the current trace context. * * This intentionally does not expose the internal async storage shape. */ export declare function getCurrentTraceContext(): TraceContextSnapshot | null; /** * Runs a callback with a previously captured trace context. * * Pass null or undefined to run the callback without any ambient trace context. */ export declare function withTraceContext<T>(context: TraceContextSnapshot | null | undefined, fn: () => T): T; /** * This function will create a new trace and assign it to the execution context of the function * passed to it. * * @param fn - The function to run and assign the trace context to. * @param options - Options for the creation of the trace */ export declare function withTrace<T>(trace: string | Trace, fn: (trace: Trace) => Promise<T>, options?: TraceOptions): Promise<T>; /** * This function will check if there is an existing active trace in the execution context. If there * is, it will run the given function with the existing trace. If there is no trace, it will create * a new one and assign it to the execution context of the function. * * @param fn - The fzunction to run and assign the trace context to. * @param options - Options for the creation of the trace */ export declare function getOrCreateTrace<T>(fn: () => Promise<T>, options?: TraceOptions): Promise<T>; /** * This function will set the current span in the execution context. * * @param span - The span to set as the current span. */ export declare function setCurrentSpan(span: Span<any>): void; export declare function resetCurrentSpan(): void; /** * This function will add an error to the current span. * * @param spanError - The error to add to the current span. */ export declare function addErrorToCurrentSpan(spanError: SpanError): void; /** * This function will clone the current context by creating new instances of the trace, span, and * previous span. * * @param context - The context to clone. * @returns A clone of the context. */ export declare function cloneCurrentContext(context: ContextState): { trace: Trace | undefined; span: Span<any> | undefined; previousSpan: Span<any> | undefined; active: boolean; }; /** * This function will run the given function with a new span context. * * @param fn - The function to run with the new span context. */ export declare function withNewSpanContext<T>(fn: () => Promise<T>): Promise<T>; export {};