@openai/agents-core
Version:
The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.
303 lines • 8.29 kB
JavaScript
import { randomUUID } from '@openai/agents-core/_shims';
import { toSmartString } from "./utils/smartString.mjs";
import { getFunctionToolStateKeyForCall, getToolCallQualifiedName, } from "./toolIdentity.mjs";
export class RunItemBase {
type = 'base_item';
rawItem;
toJSON() {
return {
type: this.type,
rawItem: this.rawItem,
};
}
}
/**
* An input item admitted while resuming a run.
*/
export class RunInputItem extends RunItemBase {
rawItem;
agent;
inputId;
type = 'input_item';
constructor(rawItem, agent, inputId = randomUUID()) {
super();
this.rawItem = rawItem;
this.agent = agent;
this.inputId = inputId;
}
toJSON() {
return {
...super.toJSON(),
agent: this.agent.toJSON(),
inputId: this.inputId,
};
}
}
export 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;
}
}
export 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'));
}
}
export 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(),
};
}
}
export 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(),
};
}
}
export 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: toSmartString(this.output),
customData: this.customData,
executionStatus: this.executionStatus,
};
}
get callId() {
return (getStringProperty(this.rawItem, 'callId') ??
getStringProperty(this.rawItem, 'id'));
}
}
export 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(),
};
}
}
/**
* A compaction marker returned by a model.
*/
export 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(),
};
}
}
export 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(),
};
}
}
export 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(),
};
}
}
export 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,
};
}
}
function getDefaultApprovalFunctionToolStateKey(rawItem) {
if (rawItem.type !== 'function_call') {
return undefined;
}
return 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 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.
*/
export function extractAllTextOutput(items) {
return items
.filter((item) => item.type === 'message_output_item')
.map((item) => item.content)
.join('');
}
//# sourceMappingURL=items.mjs.map