@openai/agents-core 0.0.16 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent.d.ts +5 -3
- package/dist/agent.js +46 -23
- package/dist/agent.js.map +1 -1
- package/dist/agent.mjs +17 -1
- package/dist/agent.mjs.map +1 -1
- package/dist/computer.js +2 -1
- package/dist/config.js +12 -6
- package/dist/config.js.map +1 -1
- package/dist/defaultModel.d.ts +22 -0
- package/dist/defaultModel.js +56 -0
- package/dist/defaultModel.js.map +1 -0
- package/dist/defaultModel.mjs +49 -0
- package/dist/defaultModel.mjs.map +1 -0
- package/dist/errors.js +21 -9
- package/dist/errors.js.map +1 -1
- package/dist/events.js +9 -3
- package/dist/events.js.map +1 -1
- package/dist/extensions/handoffFilters.js +9 -6
- package/dist/extensions/handoffFilters.js.map +1 -1
- package/dist/extensions/handoffPrompt.js +7 -3
- package/dist/extensions/handoffPrompt.js.map +1 -1
- package/dist/extensions/index.js +8 -2
- package/dist/extensions/index.js.map +1 -1
- package/dist/guardrail.js +6 -2
- package/dist/guardrail.js.map +1 -1
- package/dist/handoff.js +28 -18
- package/dist/handoff.js.map +1 -1
- package/dist/helpers/message.js +8 -3
- package/dist/helpers/message.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +127 -25
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/items.js +23 -11
- package/dist/items.js.map +1 -1
- package/dist/lifecycle.js +12 -6
- package/dist/lifecycle.js.map +1 -1
- package/dist/logger.js +15 -8
- package/dist/logger.js.map +1 -1
- package/dist/mcp.d.ts +1 -1
- package/dist/mcp.js +60 -45
- package/dist/mcp.js.map +1 -1
- package/dist/mcp.mjs +1 -1
- package/dist/mcp.mjs.map +1 -1
- package/dist/mcpUtil.js +4 -1
- package/dist/mcpUtil.js.map +1 -1
- package/dist/metadata.d.ts +0 -1
- package/dist/metadata.js +8 -6
- package/dist/metadata.js.map +1 -1
- package/dist/metadata.mjs +3 -4
- package/dist/metadata.mjs.map +1 -1
- package/dist/model.d.ts +7 -0
- package/dist/model.js +2 -1
- package/dist/providers.js +6 -2
- package/dist/providers.js.map +1 -1
- package/dist/result.js +22 -14
- package/dist/result.js.map +1 -1
- package/dist/run.d.ts +1 -0
- package/dist/run.js +145 -95
- package/dist/run.js.map +1 -1
- package/dist/run.mjs +39 -0
- package/dist/run.mjs.map +1 -1
- package/dist/runContext.d.ts +1 -1
- package/dist/runContext.js +14 -7
- package/dist/runContext.js.map +1 -1
- package/dist/runContext.mjs +1 -1
- package/dist/runImplementation.d.ts +1 -1
- package/dist/runImplementation.js +107 -90
- package/dist/runImplementation.js.map +1 -1
- package/dist/runImplementation.mjs +2 -2
- package/dist/runImplementation.mjs.map +1 -1
- package/dist/runState.d.ts +7 -1
- package/dist/runState.js +204 -151
- package/dist/runState.js.map +1 -1
- package/dist/runState.mjs +10 -1
- package/dist/runState.mjs.map +1 -1
- package/dist/shims/interface.js +2 -1
- package/dist/shims/mcp-server/browser.js +10 -4
- package/dist/shims/mcp-server/browser.js.map +1 -1
- package/dist/shims/mcp-server/node.js +67 -25
- package/dist/shims/mcp-server/node.js.map +1 -1
- package/dist/shims/shims-browser.js +25 -13
- package/dist/shims/shims-browser.js.map +1 -1
- package/dist/shims/shims-node.js +72 -15
- package/dist/shims/shims-node.js.map +1 -1
- package/dist/shims/shims-node.mjs +11 -3
- package/dist/shims/shims-node.mjs.map +1 -1
- package/dist/shims/shims-workerd.js +77 -18
- package/dist/shims/shims-workerd.js.map +1 -1
- package/dist/shims/shims-workerd.mjs +11 -3
- package/dist/shims/shims-workerd.mjs.map +1 -1
- package/dist/shims/shims.js +17 -1
- package/dist/shims/shims.js.map +1 -1
- package/dist/tool.d.ts +11 -4
- package/dist/tool.js +121 -54
- package/dist/tool.js.map +1 -1
- package/dist/tool.mjs +84 -25
- package/dist/tool.mjs.map +1 -1
- package/dist/tracing/context.js +25 -14
- package/dist/tracing/context.js.map +1 -1
- package/dist/tracing/createSpans.js +52 -38
- package/dist/tracing/createSpans.js.map +1 -1
- package/dist/tracing/index.js +51 -15
- package/dist/tracing/index.js.map +1 -1
- package/dist/tracing/processor.js +32 -21
- package/dist/tracing/processor.js.map +1 -1
- package/dist/tracing/processor.mjs +1 -1
- package/dist/tracing/processor.mjs.map +1 -1
- package/dist/tracing/provider.js +53 -45
- package/dist/tracing/provider.js.map +1 -1
- package/dist/tracing/spans.js +18 -10
- package/dist/tracing/spans.js.map +1 -1
- package/dist/tracing/traces.js +11 -6
- package/dist/tracing/traces.js.map +1 -1
- package/dist/tracing/utils.js +16 -9
- package/dist/tracing/utils.js.map +1 -1
- package/dist/types/aliases.js +2 -1
- package/dist/types/helpers.d.ts +1 -1
- package/dist/types/helpers.js +2 -1
- package/dist/types/index.js +44 -5
- package/dist/types/index.js.map +1 -1
- package/dist/types/protocol.d.ts +1 -1
- package/dist/types/protocol.js +188 -185
- package/dist/types/protocol.js.map +1 -1
- package/dist/types/protocol.mjs +1 -1
- package/dist/types/protocol.mjs.map +1 -1
- package/dist/types/providerData.d.ts +11 -3
- package/dist/types/providerData.js +2 -1
- package/dist/usage.js +7 -3
- package/dist/usage.js.map +1 -1
- package/dist/utils/index.js +9 -3
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/messages.js +6 -2
- package/dist/utils/messages.js.map +1 -1
- package/dist/utils/safeExecute.js +4 -1
- package/dist/utils/safeExecute.js.map +1 -1
- package/dist/utils/serialize.js +6 -2
- package/dist/utils/serialize.js.map +1 -1
- package/dist/utils/smartString.js +4 -1
- package/dist/utils/smartString.js.map +1 -1
- package/dist/utils/tools.js +16 -11
- package/dist/utils/tools.js.map +1 -1
- package/dist/utils/typeGuards.d.ts +1 -1
- package/dist/utils/typeGuards.js +6 -2
- package/dist/utils/typeGuards.js.map +1 -1
- package/package.json +24 -43
package/dist/run.js
CHANGED
|
@@ -1,26 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Runner = void 0;
|
|
7
|
+
exports.getTracing = getTracing;
|
|
8
|
+
exports.getTurnInput = getTurnInput;
|
|
9
|
+
exports.selectModel = selectModel;
|
|
10
|
+
exports.run = run;
|
|
11
|
+
const agent_1 = require("./agent.js");
|
|
12
|
+
const guardrail_1 = require("./guardrail.js");
|
|
13
|
+
const handoff_1 = require("./handoff.js");
|
|
14
|
+
const providers_1 = require("./providers.js");
|
|
15
|
+
const runContext_1 = require("./runContext.js");
|
|
16
|
+
const result_1 = require("./result.js");
|
|
17
|
+
const lifecycle_1 = require("./lifecycle.js");
|
|
18
|
+
const logger_1 = __importDefault(require("./logger.js"));
|
|
19
|
+
const serialize_1 = require("./utils/serialize.js");
|
|
20
|
+
const errors_1 = require("./errors.js");
|
|
21
|
+
const runImplementation_1 = require("./runImplementation.js");
|
|
22
|
+
const context_1 = require("./tracing/context.js");
|
|
23
|
+
const tracing_1 = require("./tracing/index.js");
|
|
24
|
+
const usage_1 = require("./usage.js");
|
|
25
|
+
const events_1 = require("./events.js");
|
|
26
|
+
const runState_1 = require("./runState.js");
|
|
27
|
+
const protocol_1 = require("./types/protocol.js");
|
|
28
|
+
const tools_1 = require("./utils/tools.js");
|
|
29
|
+
const defaultModel_1 = require("./defaultModel.js");
|
|
19
30
|
const DEFAULT_MAX_TURNS = 10;
|
|
20
31
|
/**
|
|
21
32
|
* @internal
|
|
22
33
|
*/
|
|
23
|
-
|
|
34
|
+
function getTracing(tracingDisabled, traceIncludeSensitiveData) {
|
|
24
35
|
if (tracingDisabled) {
|
|
25
36
|
return false;
|
|
26
37
|
}
|
|
@@ -29,7 +40,7 @@ export function getTracing(tracingDisabled, traceIncludeSensitiveData) {
|
|
|
29
40
|
}
|
|
30
41
|
return 'enabled_without_data';
|
|
31
42
|
}
|
|
32
|
-
|
|
43
|
+
function getTurnInput(originalInput, generatedItems) {
|
|
33
44
|
const rawItems = generatedItems
|
|
34
45
|
.filter((item) => item.type !== 'tool_approval_item') // don't include approval items to avoid double function calls
|
|
35
46
|
.map((item) => item.rawItem);
|
|
@@ -41,14 +52,14 @@ export function getTurnInput(originalInput, generatedItems) {
|
|
|
41
52
|
/**
|
|
42
53
|
* A Runner is responsible for running an agent workflow.
|
|
43
54
|
*/
|
|
44
|
-
|
|
55
|
+
class Runner extends lifecycle_1.RunHooks {
|
|
45
56
|
config;
|
|
46
57
|
inputGuardrailDefs;
|
|
47
58
|
outputGuardrailDefs;
|
|
48
59
|
constructor(config = {}) {
|
|
49
60
|
super();
|
|
50
61
|
this.config = {
|
|
51
|
-
modelProvider: config.modelProvider ?? getDefaultModelProvider(),
|
|
62
|
+
modelProvider: config.modelProvider ?? (0, providers_1.getDefaultModelProvider)(),
|
|
52
63
|
model: config.model,
|
|
53
64
|
modelSettings: config.modelSettings,
|
|
54
65
|
handoffInputFilter: config.handoffInputFilter,
|
|
@@ -61,22 +72,25 @@ export class Runner extends RunHooks {
|
|
|
61
72
|
groupId: config.groupId,
|
|
62
73
|
traceMetadata: config.traceMetadata,
|
|
63
74
|
};
|
|
64
|
-
this.inputGuardrailDefs = (config.inputGuardrails ?? []).map(defineInputGuardrail);
|
|
65
|
-
this.outputGuardrailDefs = (config.outputGuardrails ?? []).map(defineOutputGuardrail);
|
|
75
|
+
this.inputGuardrailDefs = (config.inputGuardrails ?? []).map(guardrail_1.defineInputGuardrail);
|
|
76
|
+
this.outputGuardrailDefs = (config.outputGuardrails ?? []).map(guardrail_1.defineOutputGuardrail);
|
|
66
77
|
}
|
|
67
78
|
/**
|
|
68
79
|
* @internal
|
|
69
80
|
*/
|
|
70
81
|
async #runIndividualNonStream(startingAgent, input, options) {
|
|
71
|
-
return withNewSpanContext(async () => {
|
|
82
|
+
return (0, context_1.withNewSpanContext)(async () => {
|
|
72
83
|
// if we have a saved state we use that one, otherwise we create a new one
|
|
73
|
-
const state = input instanceof RunState
|
|
84
|
+
const state = input instanceof runState_1.RunState
|
|
74
85
|
? input
|
|
75
|
-
: new RunState(options.context instanceof RunContext
|
|
86
|
+
: new runState_1.RunState(options.context instanceof runContext_1.RunContext
|
|
76
87
|
? options.context
|
|
77
|
-
: new RunContext(options.context), input, startingAgent, options.maxTurns ?? DEFAULT_MAX_TURNS);
|
|
88
|
+
: new runContext_1.RunContext(options.context), input, startingAgent, options.maxTurns ?? DEFAULT_MAX_TURNS);
|
|
78
89
|
try {
|
|
79
90
|
while (true) {
|
|
91
|
+
const explictlyModelSet = (state._currentAgent.model !== undefined &&
|
|
92
|
+
state._currentAgent.model !== '') ||
|
|
93
|
+
(this.config.model !== undefined && this.config.model !== '');
|
|
80
94
|
let model = selectModel(state._currentAgent.model, this.config.model);
|
|
81
95
|
if (typeof model === 'string') {
|
|
82
96
|
model = await this.config.modelProvider.getModel(model);
|
|
@@ -86,18 +100,18 @@ export class Runner extends RunHooks {
|
|
|
86
100
|
type: 'next_step_run_again',
|
|
87
101
|
};
|
|
88
102
|
if (state._currentStep.type === 'next_step_interruption') {
|
|
89
|
-
|
|
103
|
+
logger_1.default.debug('Continuing from interruption');
|
|
90
104
|
if (!state._lastTurnResponse || !state._lastProcessedResponse) {
|
|
91
|
-
throw new UserError('No model response found in previous state', state);
|
|
105
|
+
throw new errors_1.UserError('No model response found in previous state', state);
|
|
92
106
|
}
|
|
93
|
-
const turnResult = await executeInterruptedToolsAndSideEffects(state._currentAgent, state._originalInput, state._generatedItems, state._lastTurnResponse, state._lastProcessedResponse, this, state);
|
|
107
|
+
const turnResult = await (0, runImplementation_1.executeInterruptedToolsAndSideEffects)(state._currentAgent, state._originalInput, state._generatedItems, state._lastTurnResponse, state._lastProcessedResponse, this, state);
|
|
94
108
|
state._toolUseTracker.addToolUse(state._currentAgent, state._lastProcessedResponse.toolsUsed);
|
|
95
109
|
state._originalInput = turnResult.originalInput;
|
|
96
110
|
state._generatedItems = turnResult.generatedItems;
|
|
97
111
|
state._currentStep = turnResult.nextStep;
|
|
98
112
|
if (turnResult.nextStep.type === 'next_step_interruption') {
|
|
99
113
|
// we are still in an interruption, so we need to avoid an infinite loop
|
|
100
|
-
return new RunResult(state);
|
|
114
|
+
return new result_1.RunResult(state);
|
|
101
115
|
}
|
|
102
116
|
continue;
|
|
103
117
|
}
|
|
@@ -106,11 +120,11 @@ export class Runner extends RunHooks {
|
|
|
106
120
|
if (state._currentAgent.handoffs) {
|
|
107
121
|
// While this array usually must not be undefined,
|
|
108
122
|
// we've added this check to prevent unexpected runtime errors like https://github.com/openai/openai-agents-js/issues/138
|
|
109
|
-
handoffs.push(...state._currentAgent.handoffs.map(getHandoff));
|
|
123
|
+
handoffs.push(...state._currentAgent.handoffs.map(handoff_1.getHandoff));
|
|
110
124
|
}
|
|
111
125
|
if (!state._currentAgentSpan) {
|
|
112
126
|
const handoffNames = handoffs.map((h) => h.agentName);
|
|
113
|
-
state._currentAgentSpan = createAgentSpan({
|
|
127
|
+
state._currentAgentSpan = (0, tracing_1.createAgentSpan)({
|
|
114
128
|
data: {
|
|
115
129
|
name: state._currentAgent.name,
|
|
116
130
|
handoffs: handoffNames,
|
|
@@ -118,11 +132,11 @@ export class Runner extends RunHooks {
|
|
|
118
132
|
},
|
|
119
133
|
});
|
|
120
134
|
state._currentAgentSpan.start();
|
|
121
|
-
setCurrentSpan(state._currentAgentSpan);
|
|
135
|
+
(0, context_1.setCurrentSpan)(state._currentAgentSpan);
|
|
122
136
|
}
|
|
123
137
|
const tools = await state._currentAgent.getAllTools(state._context);
|
|
124
|
-
const serializedTools = tools.map((t) => serializeTool(t));
|
|
125
|
-
const serializedHandoffs = handoffs.map((h) => serializeHandoff(h));
|
|
138
|
+
const serializedTools = tools.map((t) => (0, serialize_1.serializeTool)(t));
|
|
139
|
+
const serializedHandoffs = handoffs.map((h) => (0, serialize_1.serializeHandoff)(h));
|
|
126
140
|
if (state._currentAgentSpan) {
|
|
127
141
|
state._currentAgentSpan.spanData.tools = tools.map((t) => t.name);
|
|
128
142
|
}
|
|
@@ -132,9 +146,9 @@ export class Runner extends RunHooks {
|
|
|
132
146
|
message: 'Max turns exceeded',
|
|
133
147
|
data: { max_turns: state._maxTurns },
|
|
134
148
|
});
|
|
135
|
-
throw new MaxTurnsExceededError(`Max turns (${state._maxTurns}) exceeded`, state);
|
|
149
|
+
throw new errors_1.MaxTurnsExceededError(`Max turns (${state._maxTurns}) exceeded`, state);
|
|
136
150
|
}
|
|
137
|
-
|
|
151
|
+
logger_1.default.debug(`Running agent ${state._currentAgent.name} (turn ${state._currentTurn})`);
|
|
138
152
|
if (state._currentTurn === 1) {
|
|
139
153
|
await this.#runInputGuardrails(state);
|
|
140
154
|
}
|
|
@@ -147,15 +161,18 @@ export class Runner extends RunHooks {
|
|
|
147
161
|
...this.config.modelSettings,
|
|
148
162
|
...state._currentAgent.modelSettings,
|
|
149
163
|
};
|
|
150
|
-
|
|
164
|
+
const agentModelSettings = state._currentAgent.modelSettings;
|
|
165
|
+
modelSettings = adjustModelSettingsForNonGPT5RunnerModel(explictlyModelSet, agentModelSettings, model, modelSettings);
|
|
166
|
+
modelSettings = (0, runImplementation_1.maybeResetToolChoice)(state._currentAgent, state._toolUseTracker, modelSettings);
|
|
151
167
|
state._lastTurnResponse = await model.getResponse({
|
|
152
168
|
systemInstructions: await state._currentAgent.getSystemPrompt(state._context),
|
|
153
169
|
prompt: await state._currentAgent.getPrompt(state._context),
|
|
154
170
|
input: turnInput,
|
|
155
171
|
previousResponseId: options.previousResponseId,
|
|
172
|
+
conversationId: options.conversationId,
|
|
156
173
|
modelSettings,
|
|
157
174
|
tools: serializedTools,
|
|
158
|
-
outputType: convertAgentOutputTypeToSerializable(state._currentAgent.outputType),
|
|
175
|
+
outputType: (0, tools_1.convertAgentOutputTypeToSerializable)(state._currentAgent.outputType),
|
|
159
176
|
handoffs: serializedHandoffs,
|
|
160
177
|
tracing: getTracing(this.config.tracingDisabled, this.config.traceIncludeSensitiveData),
|
|
161
178
|
signal: options.signal,
|
|
@@ -163,9 +180,9 @@ export class Runner extends RunHooks {
|
|
|
163
180
|
state._modelResponses.push(state._lastTurnResponse);
|
|
164
181
|
state._context.usage.add(state._lastTurnResponse.usage);
|
|
165
182
|
state._noActiveAgentRun = false;
|
|
166
|
-
const processedResponse = processModelResponse(state._lastTurnResponse, state._currentAgent, tools, handoffs);
|
|
183
|
+
const processedResponse = (0, runImplementation_1.processModelResponse)(state._lastTurnResponse, state._currentAgent, tools, handoffs);
|
|
167
184
|
state._lastProcessedResponse = processedResponse;
|
|
168
|
-
const turnResult = await executeToolsAndSideEffects(state._currentAgent, state._originalInput, state._generatedItems, state._lastTurnResponse, state._lastProcessedResponse, this, state);
|
|
185
|
+
const turnResult = await (0, runImplementation_1.executeToolsAndSideEffects)(state._currentAgent, state._originalInput, state._generatedItems, state._lastTurnResponse, state._lastProcessedResponse, this, state);
|
|
169
186
|
state._toolUseTracker.addToolUse(state._currentAgent, state._lastProcessedResponse.toolsUsed);
|
|
170
187
|
state._originalInput = turnResult.originalInput;
|
|
171
188
|
state._generatedItems = turnResult.generatedItems;
|
|
@@ -176,14 +193,14 @@ export class Runner extends RunHooks {
|
|
|
176
193
|
await this.#runOutputGuardrails(state, state._currentStep.output);
|
|
177
194
|
this.emit('agent_end', state._context, state._currentAgent, state._currentStep.output);
|
|
178
195
|
state._currentAgent.emit('agent_end', state._context, state._currentStep.output);
|
|
179
|
-
return new RunResult(state);
|
|
196
|
+
return new result_1.RunResult(state);
|
|
180
197
|
}
|
|
181
198
|
else if (state._currentStep &&
|
|
182
199
|
state._currentStep.type === 'next_step_handoff') {
|
|
183
200
|
state._currentAgent = state._currentStep.newAgent;
|
|
184
201
|
if (state._currentAgentSpan) {
|
|
185
202
|
state._currentAgentSpan.end();
|
|
186
|
-
resetCurrentSpan();
|
|
203
|
+
(0, context_1.resetCurrentSpan)();
|
|
187
204
|
state._currentAgentSpan = undefined;
|
|
188
205
|
}
|
|
189
206
|
state._noActiveAgentRun = true;
|
|
@@ -193,10 +210,10 @@ export class Runner extends RunHooks {
|
|
|
193
210
|
else if (state._currentStep &&
|
|
194
211
|
state._currentStep.type === 'next_step_interruption') {
|
|
195
212
|
// interrupted. Don't run any guardrails
|
|
196
|
-
return new RunResult(state);
|
|
213
|
+
return new result_1.RunResult(state);
|
|
197
214
|
}
|
|
198
215
|
else {
|
|
199
|
-
|
|
216
|
+
logger_1.default.debug('Running next loop');
|
|
200
217
|
}
|
|
201
218
|
}
|
|
202
219
|
}
|
|
@@ -215,13 +232,13 @@ export class Runner extends RunHooks {
|
|
|
215
232
|
// don't end the span if the run was interrupted
|
|
216
233
|
state._currentAgentSpan.end();
|
|
217
234
|
}
|
|
218
|
-
resetCurrentSpan();
|
|
235
|
+
(0, context_1.resetCurrentSpan)();
|
|
219
236
|
}
|
|
220
237
|
}
|
|
221
238
|
});
|
|
222
239
|
}
|
|
223
240
|
async #runInputGuardrails(state) {
|
|
224
|
-
const guardrails = this.inputGuardrailDefs.concat(state._currentAgent.inputGuardrails.map(defineInputGuardrail));
|
|
241
|
+
const guardrails = this.inputGuardrailDefs.concat(state._currentAgent.inputGuardrails.map(guardrail_1.defineInputGuardrail));
|
|
225
242
|
if (guardrails.length > 0) {
|
|
226
243
|
const guardrailArgs = {
|
|
227
244
|
agent: state._currentAgent,
|
|
@@ -230,7 +247,7 @@ export class Runner extends RunHooks {
|
|
|
230
247
|
};
|
|
231
248
|
try {
|
|
232
249
|
const results = await Promise.all(guardrails.map(async (guardrail) => {
|
|
233
|
-
return withGuardrailSpan(async (span) => {
|
|
250
|
+
return (0, tracing_1.withGuardrailSpan)(async (span) => {
|
|
234
251
|
const result = await guardrail.run(guardrailArgs);
|
|
235
252
|
span.spanData.triggered = result.output.tripwireTriggered;
|
|
236
253
|
return result;
|
|
@@ -244,22 +261,22 @@ export class Runner extends RunHooks {
|
|
|
244
261
|
data: { guardrail: result.guardrail.name },
|
|
245
262
|
});
|
|
246
263
|
}
|
|
247
|
-
throw new InputGuardrailTripwireTriggered(`Input guardrail triggered: ${JSON.stringify(result.output.outputInfo)}`, result, state);
|
|
264
|
+
throw new errors_1.InputGuardrailTripwireTriggered(`Input guardrail triggered: ${JSON.stringify(result.output.outputInfo)}`, result, state);
|
|
248
265
|
}
|
|
249
266
|
}
|
|
250
267
|
}
|
|
251
268
|
catch (e) {
|
|
252
|
-
if (e instanceof InputGuardrailTripwireTriggered) {
|
|
269
|
+
if (e instanceof errors_1.InputGuardrailTripwireTriggered) {
|
|
253
270
|
throw e;
|
|
254
271
|
}
|
|
255
272
|
// roll back the current turn to enable reruns
|
|
256
273
|
state._currentTurn--;
|
|
257
|
-
throw new GuardrailExecutionError(`Input guardrail failed to complete: ${e}`, e, state);
|
|
274
|
+
throw new errors_1.GuardrailExecutionError(`Input guardrail failed to complete: ${e}`, e, state);
|
|
258
275
|
}
|
|
259
276
|
}
|
|
260
277
|
}
|
|
261
278
|
async #runOutputGuardrails(state, output) {
|
|
262
|
-
const guardrails = this.outputGuardrailDefs.concat(state._currentAgent.outputGuardrails.map(defineOutputGuardrail));
|
|
279
|
+
const guardrails = this.outputGuardrailDefs.concat(state._currentAgent.outputGuardrails.map(guardrail_1.defineOutputGuardrail));
|
|
263
280
|
if (guardrails.length > 0) {
|
|
264
281
|
const agentOutput = state._currentAgent.processFinalOutput(output);
|
|
265
282
|
const guardrailArgs = {
|
|
@@ -270,7 +287,7 @@ export class Runner extends RunHooks {
|
|
|
270
287
|
};
|
|
271
288
|
try {
|
|
272
289
|
const results = await Promise.all(guardrails.map(async (guardrail) => {
|
|
273
|
-
return withGuardrailSpan(async (span) => {
|
|
290
|
+
return (0, tracing_1.withGuardrailSpan)(async (span) => {
|
|
274
291
|
const result = await guardrail.run(guardrailArgs);
|
|
275
292
|
span.spanData.triggered = result.output.tripwireTriggered;
|
|
276
293
|
return result;
|
|
@@ -284,15 +301,15 @@ export class Runner extends RunHooks {
|
|
|
284
301
|
data: { guardrail: result.guardrail.name },
|
|
285
302
|
});
|
|
286
303
|
}
|
|
287
|
-
throw new OutputGuardrailTripwireTriggered(`Output guardrail triggered: ${JSON.stringify(result.output.outputInfo)}`, result, state);
|
|
304
|
+
throw new errors_1.OutputGuardrailTripwireTriggered(`Output guardrail triggered: ${JSON.stringify(result.output.outputInfo)}`, result, state);
|
|
288
305
|
}
|
|
289
306
|
}
|
|
290
307
|
}
|
|
291
308
|
catch (e) {
|
|
292
|
-
if (e instanceof OutputGuardrailTripwireTriggered) {
|
|
309
|
+
if (e instanceof errors_1.OutputGuardrailTripwireTriggered) {
|
|
293
310
|
throw e;
|
|
294
311
|
}
|
|
295
|
-
throw new GuardrailExecutionError(`Output guardrail failed to complete: ${e}`, e, state);
|
|
312
|
+
throw new errors_1.GuardrailExecutionError(`Output guardrail failed to complete: ${e}`, e, state);
|
|
296
313
|
}
|
|
297
314
|
}
|
|
298
315
|
}
|
|
@@ -303,21 +320,21 @@ export class Runner extends RunHooks {
|
|
|
303
320
|
try {
|
|
304
321
|
while (true) {
|
|
305
322
|
const currentAgent = result.state._currentAgent;
|
|
306
|
-
const handoffs = currentAgent.handoffs.map(getHandoff);
|
|
323
|
+
const handoffs = currentAgent.handoffs.map(handoff_1.getHandoff);
|
|
307
324
|
const tools = await currentAgent.getAllTools(result.state._context);
|
|
308
|
-
const serializedTools = tools.map((t) => serializeTool(t));
|
|
309
|
-
const serializedHandoffs = handoffs.map((h) => serializeHandoff(h));
|
|
325
|
+
const serializedTools = tools.map((t) => (0, serialize_1.serializeTool)(t));
|
|
326
|
+
const serializedHandoffs = handoffs.map((h) => (0, serialize_1.serializeHandoff)(h));
|
|
310
327
|
result.state._currentStep = result.state._currentStep ?? {
|
|
311
328
|
type: 'next_step_run_again',
|
|
312
329
|
};
|
|
313
330
|
if (result.state._currentStep.type === 'next_step_interruption') {
|
|
314
|
-
|
|
331
|
+
logger_1.default.debug('Continuing from interruption');
|
|
315
332
|
if (!result.state._lastTurnResponse ||
|
|
316
333
|
!result.state._lastProcessedResponse) {
|
|
317
|
-
throw new UserError('No model response found in previous state', result.state);
|
|
334
|
+
throw new errors_1.UserError('No model response found in previous state', result.state);
|
|
318
335
|
}
|
|
319
|
-
const turnResult = await executeInterruptedToolsAndSideEffects(result.state._currentAgent, result.state._originalInput, result.state._generatedItems, result.state._lastTurnResponse, result.state._lastProcessedResponse, this, result.state);
|
|
320
|
-
addStepToRunResult(result, turnResult);
|
|
336
|
+
const turnResult = await (0, runImplementation_1.executeInterruptedToolsAndSideEffects)(result.state._currentAgent, result.state._originalInput, result.state._generatedItems, result.state._lastTurnResponse, result.state._lastProcessedResponse, this, result.state);
|
|
337
|
+
(0, runImplementation_1.addStepToRunResult)(result, turnResult);
|
|
321
338
|
result.state._toolUseTracker.addToolUse(result.state._currentAgent, result.state._lastProcessedResponse.toolsUsed);
|
|
322
339
|
result.state._originalInput = turnResult.originalInput;
|
|
323
340
|
result.state._generatedItems = turnResult.generatedItems;
|
|
@@ -331,7 +348,7 @@ export class Runner extends RunHooks {
|
|
|
331
348
|
if (result.state._currentStep.type === 'next_step_run_again') {
|
|
332
349
|
if (!result.state._currentAgentSpan) {
|
|
333
350
|
const handoffNames = handoffs.map((h) => h.agentName);
|
|
334
|
-
result.state._currentAgentSpan = createAgentSpan({
|
|
351
|
+
result.state._currentAgentSpan = (0, tracing_1.createAgentSpan)({
|
|
335
352
|
data: {
|
|
336
353
|
name: currentAgent.name,
|
|
337
354
|
handoffs: handoffNames,
|
|
@@ -340,7 +357,7 @@ export class Runner extends RunHooks {
|
|
|
340
357
|
},
|
|
341
358
|
});
|
|
342
359
|
result.state._currentAgentSpan.start();
|
|
343
|
-
setCurrentSpan(result.state._currentAgentSpan);
|
|
360
|
+
(0, context_1.setCurrentSpan)(result.state._currentAgentSpan);
|
|
344
361
|
}
|
|
345
362
|
result.state._currentTurn++;
|
|
346
363
|
if (result.state._currentTurn > result.state._maxTurns) {
|
|
@@ -348,9 +365,11 @@ export class Runner extends RunHooks {
|
|
|
348
365
|
message: 'Max turns exceeded',
|
|
349
366
|
data: { max_turns: result.state._maxTurns },
|
|
350
367
|
});
|
|
351
|
-
throw new MaxTurnsExceededError(`Max turns (${result.state._maxTurns}) exceeded`, result.state);
|
|
368
|
+
throw new errors_1.MaxTurnsExceededError(`Max turns (${result.state._maxTurns}) exceeded`, result.state);
|
|
352
369
|
}
|
|
353
|
-
|
|
370
|
+
logger_1.default.debug(`Running agent ${currentAgent.name} (turn ${result.state._currentTurn})`);
|
|
371
|
+
const explictlyModelSet = (currentAgent.model !== undefined && currentAgent.model !== '') ||
|
|
372
|
+
(this.config.model !== undefined && this.config.model !== '');
|
|
354
373
|
let model = selectModel(currentAgent.model, this.config.model);
|
|
355
374
|
if (typeof model === 'string') {
|
|
356
375
|
model = await this.config.modelProvider.getModel(model);
|
|
@@ -362,7 +381,9 @@ export class Runner extends RunHooks {
|
|
|
362
381
|
...this.config.modelSettings,
|
|
363
382
|
...currentAgent.modelSettings,
|
|
364
383
|
};
|
|
365
|
-
|
|
384
|
+
const agentModelSettings = currentAgent.modelSettings;
|
|
385
|
+
modelSettings = adjustModelSettingsForNonGPT5RunnerModel(explictlyModelSet, agentModelSettings, model, modelSettings);
|
|
386
|
+
modelSettings = (0, runImplementation_1.maybeResetToolChoice)(currentAgent, result.state._toolUseTracker, modelSettings);
|
|
366
387
|
const turnInput = getTurnInput(result.input, result.newItems);
|
|
367
388
|
if (result.state._noActiveAgentRun) {
|
|
368
389
|
currentAgent.emit('agent_start', result.state._context, currentAgent);
|
|
@@ -374,17 +395,18 @@ export class Runner extends RunHooks {
|
|
|
374
395
|
prompt: await currentAgent.getPrompt(result.state._context),
|
|
375
396
|
input: turnInput,
|
|
376
397
|
previousResponseId: options.previousResponseId,
|
|
398
|
+
conversationId: options.conversationId,
|
|
377
399
|
modelSettings,
|
|
378
400
|
tools: serializedTools,
|
|
379
401
|
handoffs: serializedHandoffs,
|
|
380
|
-
outputType: convertAgentOutputTypeToSerializable(currentAgent.outputType),
|
|
402
|
+
outputType: (0, tools_1.convertAgentOutputTypeToSerializable)(currentAgent.outputType),
|
|
381
403
|
tracing: getTracing(this.config.tracingDisabled, this.config.traceIncludeSensitiveData),
|
|
382
404
|
signal: options.signal,
|
|
383
405
|
})) {
|
|
384
406
|
if (event.type === 'response_done') {
|
|
385
|
-
const parsed = StreamEventResponseCompleted.parse(event);
|
|
407
|
+
const parsed = protocol_1.StreamEventResponseCompleted.parse(event);
|
|
386
408
|
finalResponse = {
|
|
387
|
-
usage: new Usage(parsed.response.usage),
|
|
409
|
+
usage: new usage_1.Usage(parsed.response.usage),
|
|
388
410
|
output: parsed.response.output,
|
|
389
411
|
responseId: parsed.response.id,
|
|
390
412
|
};
|
|
@@ -394,18 +416,18 @@ export class Runner extends RunHooks {
|
|
|
394
416
|
// this loop to prevent internal false errors and unnecessary processing
|
|
395
417
|
return;
|
|
396
418
|
}
|
|
397
|
-
result._addItem(new RunRawModelStreamEvent(event));
|
|
419
|
+
result._addItem(new events_1.RunRawModelStreamEvent(event));
|
|
398
420
|
}
|
|
399
421
|
result.state._noActiveAgentRun = false;
|
|
400
422
|
if (!finalResponse) {
|
|
401
|
-
throw new ModelBehaviorError('Model did not produce a final response!', result.state);
|
|
423
|
+
throw new errors_1.ModelBehaviorError('Model did not produce a final response!', result.state);
|
|
402
424
|
}
|
|
403
425
|
result.state._lastTurnResponse = finalResponse;
|
|
404
426
|
result.state._modelResponses.push(result.state._lastTurnResponse);
|
|
405
|
-
const processedResponse = processModelResponse(result.state._lastTurnResponse, currentAgent, tools, handoffs);
|
|
427
|
+
const processedResponse = (0, runImplementation_1.processModelResponse)(result.state._lastTurnResponse, currentAgent, tools, handoffs);
|
|
406
428
|
result.state._lastProcessedResponse = processedResponse;
|
|
407
|
-
const turnResult = await executeToolsAndSideEffects(currentAgent, result.state._originalInput, result.state._generatedItems, result.state._lastTurnResponse, result.state._lastProcessedResponse, this, result.state);
|
|
408
|
-
addStepToRunResult(result, turnResult);
|
|
429
|
+
const turnResult = await (0, runImplementation_1.executeToolsAndSideEffects)(currentAgent, result.state._originalInput, result.state._generatedItems, result.state._lastTurnResponse, result.state._lastProcessedResponse, this, result.state);
|
|
430
|
+
(0, runImplementation_1.addStepToRunResult)(result, turnResult);
|
|
409
431
|
result.state._toolUseTracker.addToolUse(currentAgent, processedResponse.toolsUsed);
|
|
410
432
|
result.state._originalInput = turnResult.originalInput;
|
|
411
433
|
result.state._generatedItems = turnResult.generatedItems;
|
|
@@ -413,6 +435,8 @@ export class Runner extends RunHooks {
|
|
|
413
435
|
}
|
|
414
436
|
if (result.state._currentStep.type === 'next_step_final_output') {
|
|
415
437
|
await this.#runOutputGuardrails(result.state, result.state._currentStep.output);
|
|
438
|
+
this.emit('agent_end', result.state._context, currentAgent, result.state._currentStep.output);
|
|
439
|
+
currentAgent.emit('agent_end', result.state._context, result.state._currentStep.output);
|
|
416
440
|
return;
|
|
417
441
|
}
|
|
418
442
|
else if (result.state._currentStep.type === 'next_step_interruption') {
|
|
@@ -424,10 +448,10 @@ export class Runner extends RunHooks {
|
|
|
424
448
|
?.newAgent;
|
|
425
449
|
if (result.state._currentAgentSpan) {
|
|
426
450
|
result.state._currentAgentSpan.end();
|
|
427
|
-
resetCurrentSpan();
|
|
451
|
+
(0, context_1.resetCurrentSpan)();
|
|
428
452
|
}
|
|
429
453
|
result.state._currentAgentSpan = undefined;
|
|
430
|
-
result._addItem(new RunAgentUpdatedStreamEvent(result.state._currentAgent));
|
|
454
|
+
result._addItem(new events_1.RunAgentUpdatedStreamEvent(result.state._currentAgent));
|
|
431
455
|
result.state._noActiveAgentRun = true;
|
|
432
456
|
// we've processed the handoff, so we need to run the loop again
|
|
433
457
|
result.state._currentStep = {
|
|
@@ -435,7 +459,7 @@ export class Runner extends RunHooks {
|
|
|
435
459
|
};
|
|
436
460
|
}
|
|
437
461
|
else {
|
|
438
|
-
|
|
462
|
+
logger_1.default.debug('Running next loop');
|
|
439
463
|
}
|
|
440
464
|
}
|
|
441
465
|
}
|
|
@@ -453,7 +477,7 @@ export class Runner extends RunHooks {
|
|
|
453
477
|
if (result.state._currentStep?.type !== 'next_step_interruption') {
|
|
454
478
|
result.state._currentAgentSpan.end();
|
|
455
479
|
}
|
|
456
|
-
resetCurrentSpan();
|
|
480
|
+
(0, context_1.resetCurrentSpan)();
|
|
457
481
|
}
|
|
458
482
|
}
|
|
459
483
|
}
|
|
@@ -462,15 +486,15 @@ export class Runner extends RunHooks {
|
|
|
462
486
|
*/
|
|
463
487
|
async #runIndividualStream(agent, input, options) {
|
|
464
488
|
options = options ?? {};
|
|
465
|
-
return withNewSpanContext(async () => {
|
|
489
|
+
return (0, context_1.withNewSpanContext)(async () => {
|
|
466
490
|
// Initialize or reuse existing state
|
|
467
|
-
const state = input instanceof RunState
|
|
491
|
+
const state = input instanceof runState_1.RunState
|
|
468
492
|
? input
|
|
469
|
-
: new RunState(options.context instanceof RunContext
|
|
493
|
+
: new runState_1.RunState(options.context instanceof runContext_1.RunContext
|
|
470
494
|
? options.context
|
|
471
|
-
: new RunContext(options.context), input, agent, options.maxTurns ?? DEFAULT_MAX_TURNS);
|
|
495
|
+
: new runContext_1.RunContext(options.context), input, agent, options.maxTurns ?? DEFAULT_MAX_TURNS);
|
|
472
496
|
// Initialize the streamed result with existing state
|
|
473
|
-
const result = new StreamedRunResult({
|
|
497
|
+
const result = new result_1.StreamedRunResult({
|
|
474
498
|
signal: options.signal,
|
|
475
499
|
state,
|
|
476
500
|
});
|
|
@@ -489,10 +513,10 @@ export class Runner extends RunHooks {
|
|
|
489
513
|
stream: false,
|
|
490
514
|
context: undefined,
|
|
491
515
|
}) {
|
|
492
|
-
if (input instanceof RunState && input._trace) {
|
|
493
|
-
return withTrace(input._trace, async () => {
|
|
516
|
+
if (input instanceof runState_1.RunState && input._trace) {
|
|
517
|
+
return (0, context_1.withTrace)(input._trace, async () => {
|
|
494
518
|
if (input._currentAgentSpan) {
|
|
495
|
-
setCurrentSpan(input._currentAgentSpan);
|
|
519
|
+
(0, context_1.setCurrentSpan)(input._currentAgentSpan);
|
|
496
520
|
}
|
|
497
521
|
if (options?.stream) {
|
|
498
522
|
return this.#runIndividualStream(agent, input, options);
|
|
@@ -502,7 +526,7 @@ export class Runner extends RunHooks {
|
|
|
502
526
|
}
|
|
503
527
|
});
|
|
504
528
|
}
|
|
505
|
-
return getOrCreateTrace(async () => {
|
|
529
|
+
return (0, context_1.getOrCreateTrace)(async () => {
|
|
506
530
|
if (options?.stream) {
|
|
507
531
|
return this.#runIndividualStream(agent, input, options);
|
|
508
532
|
}
|
|
@@ -517,6 +541,7 @@ export class Runner extends RunHooks {
|
|
|
517
541
|
});
|
|
518
542
|
}
|
|
519
543
|
}
|
|
544
|
+
exports.Runner = Runner;
|
|
520
545
|
let _defaultRunner = undefined;
|
|
521
546
|
function getDefaultRunner() {
|
|
522
547
|
if (_defaultRunner) {
|
|
@@ -525,19 +550,19 @@ function getDefaultRunner() {
|
|
|
525
550
|
_defaultRunner = new Runner();
|
|
526
551
|
return _defaultRunner;
|
|
527
552
|
}
|
|
528
|
-
|
|
553
|
+
function selectModel(agentModel, runConfigModel) {
|
|
529
554
|
// When initializing an agent without model name, the model property is set to an empty string. So,
|
|
530
555
|
// * agentModel === '' & runConfigModel exists, runConfigModel will be used
|
|
531
556
|
// * agentModel is set, the agentModel will be used over runConfigModel
|
|
532
557
|
if ((typeof agentModel === 'string' &&
|
|
533
|
-
agentModel !== Agent.DEFAULT_MODEL_PLACEHOLDER) ||
|
|
558
|
+
agentModel !== agent_1.Agent.DEFAULT_MODEL_PLACEHOLDER) ||
|
|
534
559
|
agentModel // any truthy value
|
|
535
560
|
) {
|
|
536
561
|
return agentModel;
|
|
537
562
|
}
|
|
538
|
-
return runConfigModel ?? agentModel ?? Agent.DEFAULT_MODEL_PLACEHOLDER;
|
|
563
|
+
return runConfigModel ?? agentModel ?? agent_1.Agent.DEFAULT_MODEL_PLACEHOLDER;
|
|
539
564
|
}
|
|
540
|
-
|
|
565
|
+
async function run(agent, input, options) {
|
|
541
566
|
const runner = getDefaultRunner();
|
|
542
567
|
if (options?.stream) {
|
|
543
568
|
return await runner.run(agent, input, options);
|
|
@@ -546,4 +571,29 @@ export async function run(agent, input, options) {
|
|
|
546
571
|
return await runner.run(agent, input, options);
|
|
547
572
|
}
|
|
548
573
|
}
|
|
574
|
+
/**
|
|
575
|
+
* When the default model is a GPT-5 variant, agents may carry GPT-5-specific providerData
|
|
576
|
+
* (e.g., reasoning effort, text verbosity). If a run resolves to a non-GPT-5 model and the
|
|
577
|
+
* agent relied on the default model (i.e., no explicit model set), these GPT-5-only settings
|
|
578
|
+
* are incompatible and should be stripped to avoid runtime errors.
|
|
579
|
+
*/
|
|
580
|
+
function adjustModelSettingsForNonGPT5RunnerModel(explictlyModelSet, agentModelSettings, runnerModel, modelSettings) {
|
|
581
|
+
if (
|
|
582
|
+
// gpt-5 is enabled for the default model for agents
|
|
583
|
+
(0, defaultModel_1.isGpt5Default)() &&
|
|
584
|
+
// explicitly set model for the agent
|
|
585
|
+
explictlyModelSet &&
|
|
586
|
+
// this runner uses a non-gpt-5 model
|
|
587
|
+
(typeof runnerModel !== 'string' ||
|
|
588
|
+
!(0, defaultModel_1.gpt5ReasoningSettingsRequired)(runnerModel)) &&
|
|
589
|
+
(agentModelSettings.providerData?.reasoning ||
|
|
590
|
+
agentModelSettings.providerData?.text?.verbosity ||
|
|
591
|
+
agentModelSettings.providerData?.reasoning_effort)) {
|
|
592
|
+
// the incompatible parameters should be removed to avoid runtime errors
|
|
593
|
+
delete modelSettings.providerData?.reasoning;
|
|
594
|
+
delete modelSettings.providerData?.text?.verbosity;
|
|
595
|
+
delete modelSettings.providerData?.reasoning_effort;
|
|
596
|
+
}
|
|
597
|
+
return modelSettings;
|
|
598
|
+
}
|
|
549
599
|
//# sourceMappingURL=run.js.map
|