@openai/agents-core
Version:
The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.
313 lines • 11.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.withMCPListToolsSpan = exports.withSpeechGroupSpan = exports.withSpeechSpan = exports.withTranscriptionSpan = exports.withGuardrailSpan = exports.withCustomSpan = exports.withGenerationSpan = exports.withHandoffSpan = exports.withFunctionSpan = exports.withTurnSpan = exports.withTaskSpan = exports.withAgentSpan = exports.withResponseSpan = void 0;
exports.createResponseSpan = createResponseSpan;
exports.createAgentSpan = createAgentSpan;
exports.createTaskSpan = createTaskSpan;
exports.createTurnSpan = createTurnSpan;
exports.createFunctionSpan = createFunctionSpan;
exports.createHandoffSpan = createHandoffSpan;
exports.createGenerationSpan = createGenerationSpan;
exports.createCustomSpan = createCustomSpan;
exports.createGuardrailSpan = createGuardrailSpan;
exports.createTranscriptionSpan = createTranscriptionSpan;
exports.createSpeechSpan = createSpeechSpan;
exports.createSpeechGroupSpan = createSpeechGroupSpan;
exports.createMCPListToolsSpan = createMCPListToolsSpan;
const result_1 = require("../result.js");
const context_1 = require("./context.js");
const provider_1 = require("./provider.js");
function setSpanError(span, error) {
const errorRecord = typeof error === 'object' && error !== null
? error
: undefined;
span.setError({
message: typeof errorRecord?.message === 'string'
? errorRecord.message
: String(error),
data: errorRecord?.data,
});
}
function _withSpanFactory(createSpan) {
return async (fn, ...args) => {
// Creating a new span context to make sure that the previous span is correctly reset
return (0, context_1.withNewSpanContext)(async () => {
const span = createSpan(...args);
(0, context_1.setCurrentSpan)(span);
let endDeferred = false;
try {
span.start();
const result = await fn(span);
if (result instanceof result_1.StreamedRunResult) {
const streamLoopPromise = result._getStreamLoopPromise();
if (streamLoopPromise) {
endDeferred = true;
(0, context_1.resetCurrentSpan)();
void streamLoopPromise.then(() => {
if (result.error !== null && result.error !== undefined) {
setSpanError(span, result.error);
}
span.end();
}, (error) => {
setSpanError(span, error);
span.end();
});
}
}
return result;
}
catch (error) {
if (span.error === null) {
setSpanError(span, error);
}
throw error;
}
finally {
if (!endDeferred) {
span.end();
(0, context_1.resetCurrentSpan)();
}
}
});
};
}
/**
* Create a new response span. The span will not be started automatically, you should either
* use `withResponseSpan()` or call `span.start()` and `span.end()` manually.
*
* This span captures the details of a model response, primarily the response identifier.
* If you need to capture detailed generation information such as input/output messages,
* model configuration, or usage data, use `createGenerationSpan()` instead.
*
* @param options - Optional span creation options, including span data and identifiers.
* @param parent - The parent span or trace. If not provided, the current trace/span will be used
* automatically.
*
* @returns The newly created response span.
*/
function createResponseSpan(options, parent) {
options = {};
return (0, provider_1.getGlobalTraceProvider)().createSpan({
...options,
data: {
type: 'response',
...options.data,
},
}, parent);
}
/**
* Create a new response span and automatically start and end it.
*
* This span captures the details of a model response, primarily the response identifier.
* If you need to capture detailed generation information such as input/output messages,
* model configuration, or usage data, use `generationSpan()` instead.
*/
exports.withResponseSpan = _withSpanFactory(createResponseSpan);
/**
* Create a new agent span. The span will not be started automatically, you should either
* use `withAgentSpan()` or call `span.start()` and `span.end()` manually.
*
* @param options - Optional span creation options, including span data and identifiers.
* @param parent - The parent span or trace. If not provided, the current trace/span will be used
* automatically.
*
* @returns The newly created agent span.
*/
function createAgentSpan(options, parent) {
return (0, provider_1.getGlobalTraceProvider)().createSpan({
...options,
data: {
type: 'agent',
name: options?.data?.name ?? 'Agent',
...options?.data,
},
}, parent);
}
/**
* Create a new agent span and automatically start and end it.
*/
exports.withAgentSpan = _withSpanFactory(createAgentSpan);
/**
* Create a new task span. A task represents one top-level Runner invocation.
* The span will not be started automatically.
*/
function createTaskSpan(options, parent) {
return (0, provider_1.getGlobalTraceProvider)().createSpan({
...options,
data: {
type: 'task',
name: options?.data?.name ?? 'Agent workflow',
...options?.data,
},
}, parent);
}
/** Create a task span and automatically start and end it. */
exports.withTaskSpan = _withSpanFactory(createTaskSpan);
/**
* Create a new turn span. A turn represents one agent loop iteration.
* The span will not be started automatically.
*/
function createTurnSpan(options, parent) {
return (0, provider_1.getGlobalTraceProvider)().createSpan({
...options,
data: {
type: 'turn',
...options.data,
},
}, parent);
}
/** Create a turn span and automatically start and end it. */
exports.withTurnSpan = _withSpanFactory(createTurnSpan);
/**
* Create a new function span. The span will not be started automatically, you should either
* use `withFunctionSpan()` or call `span.start()` and `span.end()` manually.
*
* @param options - Optional span creation options, including span data and identifiers.
* @param parent - The parent span or trace. If not provided, the current trace/span will be used
* automatically.
*
* @returns The newly created function span.
*/
function createFunctionSpan(options, parent) {
return (0, provider_1.getGlobalTraceProvider)().createSpan({
...options,
data: {
type: 'function',
input: options?.data?.input ?? '',
output: options?.data?.output ?? '',
...options?.data,
},
}, parent);
}
/**
* Create a new function span and automatically start and end it.
*/
exports.withFunctionSpan = _withSpanFactory(createFunctionSpan);
/**
* Create a new handoff span. The span will not be started automatically, you should either
* use `withHandoffSpan()` or call `span.start()` and `span.end()` manually.
*
* @param options - Optional span creation options, including span data and identifiers.
* @param parent - The parent span or trace. If not provided, the current trace/span will be used
* automatically.
*
* @returns The newly created handoff span.
*/
function createHandoffSpan(options, parent) {
return (0, provider_1.getGlobalTraceProvider)().createSpan({
...options,
data: { type: 'handoff', ...options?.data },
}, parent);
}
/**
* Create a new handoff span and automatically start and end it.
*/
exports.withHandoffSpan = _withSpanFactory(createHandoffSpan);
/**
* Create a new generation span. The span will not be started automatically, you should either
* use `withGenerationSpan()` or call `span.start()` and `span.end()` manually.
*
* This span captures the details of a model generation, including input/output message
* sequences, model information, and usage data. If you only need to capture a model response
* identifier, consider using `createResponseSpan()` instead.
*
* agents-core keeps generation usage payloads as provided. Backend-specific schema constraints
* are enforced by each exporter. For example, the OpenAI tracing exporter in
* `@openai/agents-openai` keeps top-level usage to `input_tokens` and
* `output_tokens` for OpenAI traces ingest, and maps additional usage fields
* under `usage.details`. Third-party exporters can apply their own mapping.
*/
function createGenerationSpan(options, parent) {
return (0, provider_1.getGlobalTraceProvider)().createSpan({
...options,
data: {
type: 'generation',
...options?.data,
},
}, parent);
}
/** Automatically create a generation span, run fn and close the span */
exports.withGenerationSpan = _withSpanFactory(createGenerationSpan);
/**
* Create a new custom span. The span will not be started automatically, you should either use
* `withCustomSpan()` or call `span.start()` and `span.end()` manually.
*/
function createCustomSpan(options, parent) {
return (0, provider_1.getGlobalTraceProvider)().createSpan({
...options,
data: {
type: 'custom',
data: {},
...options?.data,
},
}, parent);
}
exports.withCustomSpan = _withSpanFactory(createCustomSpan);
/**
* Create a new guardrail span. The span will not be started automatically, you should either use
* `withGuardrailSpan()` or call `span.start()` and `span.end()` manually.
*/
function createGuardrailSpan(options, parent) {
return (0, provider_1.getGlobalTraceProvider)().createSpan({
...options,
data: {
type: 'guardrail',
triggered: false,
...options?.data,
},
}, parent);
}
exports.withGuardrailSpan = _withSpanFactory(createGuardrailSpan);
/**
* Create a new transcription span. The span will not be started automatically.
*/
function createTranscriptionSpan(options, parent) {
return (0, provider_1.getGlobalTraceProvider)().createSpan({
...options,
data: {
type: 'transcription',
...options.data,
},
}, parent);
}
exports.withTranscriptionSpan = _withSpanFactory(createTranscriptionSpan);
/**
* Create a new speech span. The span will not be started automatically.
*/
function createSpeechSpan(options, parent) {
return (0, provider_1.getGlobalTraceProvider)().createSpan({
...options,
data: {
type: 'speech',
...options.data,
},
}, parent);
}
exports.withSpeechSpan = _withSpanFactory(createSpeechSpan);
/**
* Create a new speech group span. The span will not be started automatically.
*/
function createSpeechGroupSpan(options, parent) {
return (0, provider_1.getGlobalTraceProvider)().createSpan({
...options,
data: {
type: 'speech_group',
...options?.data,
},
}, parent);
}
exports.withSpeechGroupSpan = _withSpanFactory(createSpeechGroupSpan);
/**
* Create a new MCP list tools span. The span will not be started automatically.
*/
function createMCPListToolsSpan(options, parent) {
return (0, provider_1.getGlobalTraceProvider)().createSpan({
...options,
data: {
type: 'mcp_tools',
...options?.data,
},
}, parent);
}
exports.withMCPListToolsSpan = _withSpanFactory(createMCPListToolsSpan);
//# sourceMappingURL=createSpans.js.map