@openai/agents-core
Version:
The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.
319 lines • 9.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RunToolApprovalItem = exports.RunHandoffOutputItem = exports.RunHandoffCallItem = exports.RunCompactionItem = exports.RunReasoningItem = exports.RunToolCallOutputItem = exports.RunToolSearchOutputItem = exports.RunToolSearchCallItem = exports.RunToolCallItem = exports.RunMessageOutputItem = exports.RunInputItem = exports.RunItemBase = void 0;
exports.extractAllTextOutput = extractAllTextOutput;
const _shims_1 = require("@openai/agents-core/_shims");
const smartString_1 = require("./utils/smartString.js");
const toolIdentity_1 = require("./toolIdentity.js");
class RunItemBase {
type = 'base_item';
rawItem;
toJSON() {
return {
type: this.type,
rawItem: this.rawItem,
};
}
}
exports.RunItemBase = RunItemBase;
/**
* An input item admitted while resuming a run.
*/
class RunInputItem extends RunItemBase {
rawItem;
agent;
inputId;
type = 'input_item';
constructor(rawItem, agent, inputId = (0, _shims_1.randomUUID)()) {
super();
this.rawItem = rawItem;
this.agent = agent;
this.inputId = inputId;
}
toJSON() {
return {
...super.toJSON(),
agent: this.agent.toJSON(),
inputId: this.inputId,
};
}
}
exports.RunInputItem = RunInputItem;
class RunMessageOutputItem extends RunItemBase {
rawItem;
agent;
type = 'message_output_item';
constructor(rawItem, agent) {
super();
this.rawItem = rawItem;
this.agent = agent;
}
toJSON() {
return {
...super.toJSON(),
agent: this.agent.toJSON(),
};
}
get content() {
let content = '';
for (const part of this.rawItem.content) {
if (part.type === 'output_text') {
content += part.text;
}
}
return content;
}
}
exports.RunMessageOutputItem = RunMessageOutputItem;
class RunToolCallItem extends RunItemBase {
rawItem;
agent;
functionToolStateKey;
type = 'tool_call_item';
constructor(rawItem, agent,
/** @internal Execution-time identity for an unqualified deferred function call. */
functionToolStateKey) {
super();
this.rawItem = rawItem;
this.agent = agent;
this.functionToolStateKey = functionToolStateKey;
}
toJSON() {
return {
...super.toJSON(),
agent: this.agent.toJSON(),
...(this.functionToolStateKey !== undefined
? { functionToolStateKey: this.functionToolStateKey }
: {}),
};
}
get toolName() {
if (this.rawItem.type === 'program') {
return 'programmatic_tool_calling';
}
return getStringProperty(this.rawItem, 'name');
}
get callId() {
return (getStringProperty(this.rawItem, 'callId') ??
getStringProperty(this.rawItem, 'id'));
}
}
exports.RunToolCallItem = RunToolCallItem;
class RunToolSearchCallItem extends RunItemBase {
rawItem;
agent;
type = 'tool_search_call_item';
constructor(rawItem, agent) {
super();
this.rawItem = rawItem;
this.agent = agent;
}
toJSON() {
return {
...super.toJSON(),
agent: this.agent.toJSON(),
};
}
}
exports.RunToolSearchCallItem = RunToolSearchCallItem;
class RunToolSearchOutputItem extends RunItemBase {
rawItem;
agent;
type = 'tool_search_output_item';
constructor(rawItem, agent) {
super();
this.rawItem = rawItem;
this.agent = agent;
}
toJSON() {
return {
...super.toJSON(),
agent: this.agent.toJSON(),
};
}
}
exports.RunToolSearchOutputItem = RunToolSearchOutputItem;
class RunToolCallOutputItem extends RunItemBase {
rawItem;
agent;
output;
customData;
executionStatus;
type = 'tool_call_output_item';
constructor(rawItem, agent, output, customData, executionStatus) {
super();
this.rawItem = rawItem;
this.agent = agent;
this.output = output;
this.customData = customData;
this.executionStatus = executionStatus;
}
toJSON() {
return {
...super.toJSON(),
agent: this.agent.toJSON(),
output: (0, smartString_1.toSmartString)(this.output),
customData: this.customData,
executionStatus: this.executionStatus,
};
}
get callId() {
return (getStringProperty(this.rawItem, 'callId') ??
getStringProperty(this.rawItem, 'id'));
}
}
exports.RunToolCallOutputItem = RunToolCallOutputItem;
class RunReasoningItem extends RunItemBase {
rawItem;
agent;
type = 'reasoning_item';
constructor(rawItem, agent) {
super();
this.rawItem = rawItem;
this.agent = agent;
}
toJSON() {
return {
...super.toJSON(),
agent: this.agent.toJSON(),
};
}
}
exports.RunReasoningItem = RunReasoningItem;
/**
* A compaction marker returned by a model.
*/
class RunCompactionItem extends RunItemBase {
rawItem;
agent;
type = 'compaction_item';
constructor(rawItem, agent) {
super();
this.rawItem = rawItem;
this.agent = agent;
}
toJSON() {
return {
...super.toJSON(),
agent: this.agent.toJSON(),
};
}
}
exports.RunCompactionItem = RunCompactionItem;
class RunHandoffCallItem extends RunItemBase {
rawItem;
agent;
type = 'handoff_call_item';
constructor(rawItem, agent) {
super();
this.rawItem = rawItem;
this.agent = agent;
}
toJSON() {
return {
...super.toJSON(),
agent: this.agent.toJSON(),
};
}
}
exports.RunHandoffCallItem = RunHandoffCallItem;
class RunHandoffOutputItem extends RunItemBase {
rawItem;
sourceAgent;
targetAgent;
type = 'handoff_output_item';
constructor(rawItem, sourceAgent, targetAgent) {
super();
this.rawItem = rawItem;
this.sourceAgent = sourceAgent;
this.targetAgent = targetAgent;
}
toJSON() {
return {
...super.toJSON(),
sourceAgent: this.sourceAgent.toJSON(),
targetAgent: this.targetAgent.toJSON(),
};
}
}
exports.RunHandoffOutputItem = RunHandoffOutputItem;
class RunToolApprovalItem extends RunItemBase {
rawItem;
agent;
toolName;
functionToolStateKey;
type = 'tool_approval_item';
constructor(rawItem, agent,
/**
* Explicit tool name to use for approval tracking when not present on the raw item.
*/
toolName,
/** @internal Canonical function-tool key used by approval and resume state. */
functionToolStateKey) {
super();
this.rawItem = rawItem;
this.agent = agent;
this.toolName = toolName;
this.functionToolStateKey = functionToolStateKey;
this.toolName = toolName ?? getDefaultApprovalToolName(rawItem);
this.functionToolStateKey =
functionToolStateKey ?? getDefaultApprovalFunctionToolStateKey(rawItem);
}
/**
* Returns the tool name if available on the raw item or provided explicitly.
* Kept for backwards compatibility with code that previously relied on `rawItem.name`.
*/
get name() {
return this.toolName ?? this.rawItem.name;
}
/**
* Returns the arguments if the raw item has an arguments property otherwise this will be undefined.
*/
get arguments() {
return 'arguments' in this.rawItem ? this.rawItem.arguments : undefined;
}
toJSON() {
return {
...super.toJSON(),
agent: this.agent.toJSON(),
toolName: this.toolName,
functionToolStateKey: this.functionToolStateKey,
};
}
}
exports.RunToolApprovalItem = RunToolApprovalItem;
function getDefaultApprovalFunctionToolStateKey(rawItem) {
if (rawItem.type !== 'function_call') {
return undefined;
}
return (0, toolIdentity_1.getFunctionToolStateKeyForCall)(rawItem);
}
function getDefaultApprovalToolName(rawItem) {
if (rawItem.type !== 'function_call') {
return rawItem.name;
}
if (typeof rawItem.name === 'string' &&
typeof rawItem.namespace === 'string' &&
rawItem.namespace === rawItem.name) {
return rawItem.name;
}
return (0, toolIdentity_1.getToolCallQualifiedName)(rawItem) ?? rawItem.name;
}
function getStringProperty(item, key) {
const value = item[key];
return typeof value === 'string' ? value : undefined;
}
/**
* Extract all text output from a list of run items by concatenating the content of all
* message output items.
*
* @param items - The list of run items to extract text from.
* @returns A string of all the text output from the run items.
*/
function extractAllTextOutput(items) {
return items
.filter((item) => item.type === 'message_output_item')
.map((item) => item.content)
.join('');
}
//# sourceMappingURL=items.js.map