@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.
Files changed (147) hide show
  1. package/dist/agent.d.ts +5 -3
  2. package/dist/agent.js +46 -23
  3. package/dist/agent.js.map +1 -1
  4. package/dist/agent.mjs +17 -1
  5. package/dist/agent.mjs.map +1 -1
  6. package/dist/computer.js +2 -1
  7. package/dist/config.js +12 -6
  8. package/dist/config.js.map +1 -1
  9. package/dist/defaultModel.d.ts +22 -0
  10. package/dist/defaultModel.js +56 -0
  11. package/dist/defaultModel.js.map +1 -0
  12. package/dist/defaultModel.mjs +49 -0
  13. package/dist/defaultModel.mjs.map +1 -0
  14. package/dist/errors.js +21 -9
  15. package/dist/errors.js.map +1 -1
  16. package/dist/events.js +9 -3
  17. package/dist/events.js.map +1 -1
  18. package/dist/extensions/handoffFilters.js +9 -6
  19. package/dist/extensions/handoffFilters.js.map +1 -1
  20. package/dist/extensions/handoffPrompt.js +7 -3
  21. package/dist/extensions/handoffPrompt.js.map +1 -1
  22. package/dist/extensions/index.js +8 -2
  23. package/dist/extensions/index.js.map +1 -1
  24. package/dist/guardrail.js +6 -2
  25. package/dist/guardrail.js.map +1 -1
  26. package/dist/handoff.js +28 -18
  27. package/dist/handoff.js.map +1 -1
  28. package/dist/helpers/message.js +8 -3
  29. package/dist/helpers/message.js.map +1 -1
  30. package/dist/index.d.ts +1 -0
  31. package/dist/index.js +127 -25
  32. package/dist/index.js.map +1 -1
  33. package/dist/index.mjs +1 -0
  34. package/dist/index.mjs.map +1 -1
  35. package/dist/items.js +23 -11
  36. package/dist/items.js.map +1 -1
  37. package/dist/lifecycle.js +12 -6
  38. package/dist/lifecycle.js.map +1 -1
  39. package/dist/logger.js +15 -8
  40. package/dist/logger.js.map +1 -1
  41. package/dist/mcp.d.ts +1 -1
  42. package/dist/mcp.js +60 -45
  43. package/dist/mcp.js.map +1 -1
  44. package/dist/mcp.mjs +1 -1
  45. package/dist/mcp.mjs.map +1 -1
  46. package/dist/mcpUtil.js +4 -1
  47. package/dist/mcpUtil.js.map +1 -1
  48. package/dist/metadata.d.ts +0 -1
  49. package/dist/metadata.js +8 -6
  50. package/dist/metadata.js.map +1 -1
  51. package/dist/metadata.mjs +3 -4
  52. package/dist/metadata.mjs.map +1 -1
  53. package/dist/model.d.ts +7 -0
  54. package/dist/model.js +2 -1
  55. package/dist/providers.js +6 -2
  56. package/dist/providers.js.map +1 -1
  57. package/dist/result.js +22 -14
  58. package/dist/result.js.map +1 -1
  59. package/dist/run.d.ts +1 -0
  60. package/dist/run.js +145 -95
  61. package/dist/run.js.map +1 -1
  62. package/dist/run.mjs +39 -0
  63. package/dist/run.mjs.map +1 -1
  64. package/dist/runContext.d.ts +1 -1
  65. package/dist/runContext.js +14 -7
  66. package/dist/runContext.js.map +1 -1
  67. package/dist/runContext.mjs +1 -1
  68. package/dist/runImplementation.d.ts +1 -1
  69. package/dist/runImplementation.js +107 -90
  70. package/dist/runImplementation.js.map +1 -1
  71. package/dist/runImplementation.mjs +2 -2
  72. package/dist/runImplementation.mjs.map +1 -1
  73. package/dist/runState.d.ts +7 -1
  74. package/dist/runState.js +204 -151
  75. package/dist/runState.js.map +1 -1
  76. package/dist/runState.mjs +10 -1
  77. package/dist/runState.mjs.map +1 -1
  78. package/dist/shims/interface.js +2 -1
  79. package/dist/shims/mcp-server/browser.js +10 -4
  80. package/dist/shims/mcp-server/browser.js.map +1 -1
  81. package/dist/shims/mcp-server/node.js +67 -25
  82. package/dist/shims/mcp-server/node.js.map +1 -1
  83. package/dist/shims/shims-browser.js +25 -13
  84. package/dist/shims/shims-browser.js.map +1 -1
  85. package/dist/shims/shims-node.js +72 -15
  86. package/dist/shims/shims-node.js.map +1 -1
  87. package/dist/shims/shims-node.mjs +11 -3
  88. package/dist/shims/shims-node.mjs.map +1 -1
  89. package/dist/shims/shims-workerd.js +77 -18
  90. package/dist/shims/shims-workerd.js.map +1 -1
  91. package/dist/shims/shims-workerd.mjs +11 -3
  92. package/dist/shims/shims-workerd.mjs.map +1 -1
  93. package/dist/shims/shims.js +17 -1
  94. package/dist/shims/shims.js.map +1 -1
  95. package/dist/tool.d.ts +11 -4
  96. package/dist/tool.js +121 -54
  97. package/dist/tool.js.map +1 -1
  98. package/dist/tool.mjs +84 -25
  99. package/dist/tool.mjs.map +1 -1
  100. package/dist/tracing/context.js +25 -14
  101. package/dist/tracing/context.js.map +1 -1
  102. package/dist/tracing/createSpans.js +52 -38
  103. package/dist/tracing/createSpans.js.map +1 -1
  104. package/dist/tracing/index.js +51 -15
  105. package/dist/tracing/index.js.map +1 -1
  106. package/dist/tracing/processor.js +32 -21
  107. package/dist/tracing/processor.js.map +1 -1
  108. package/dist/tracing/processor.mjs +1 -1
  109. package/dist/tracing/processor.mjs.map +1 -1
  110. package/dist/tracing/provider.js +53 -45
  111. package/dist/tracing/provider.js.map +1 -1
  112. package/dist/tracing/spans.js +18 -10
  113. package/dist/tracing/spans.js.map +1 -1
  114. package/dist/tracing/traces.js +11 -6
  115. package/dist/tracing/traces.js.map +1 -1
  116. package/dist/tracing/utils.js +16 -9
  117. package/dist/tracing/utils.js.map +1 -1
  118. package/dist/types/aliases.js +2 -1
  119. package/dist/types/helpers.d.ts +1 -1
  120. package/dist/types/helpers.js +2 -1
  121. package/dist/types/index.js +44 -5
  122. package/dist/types/index.js.map +1 -1
  123. package/dist/types/protocol.d.ts +1 -1
  124. package/dist/types/protocol.js +188 -185
  125. package/dist/types/protocol.js.map +1 -1
  126. package/dist/types/protocol.mjs +1 -1
  127. package/dist/types/protocol.mjs.map +1 -1
  128. package/dist/types/providerData.d.ts +11 -3
  129. package/dist/types/providerData.js +2 -1
  130. package/dist/usage.js +7 -3
  131. package/dist/usage.js.map +1 -1
  132. package/dist/utils/index.js +9 -3
  133. package/dist/utils/index.js.map +1 -1
  134. package/dist/utils/messages.js +6 -2
  135. package/dist/utils/messages.js.map +1 -1
  136. package/dist/utils/safeExecute.js +4 -1
  137. package/dist/utils/safeExecute.js.map +1 -1
  138. package/dist/utils/serialize.js +6 -2
  139. package/dist/utils/serialize.js.map +1 -1
  140. package/dist/utils/smartString.js +4 -1
  141. package/dist/utils/smartString.js.map +1 -1
  142. package/dist/utils/tools.js +16 -11
  143. package/dist/utils/tools.js.map +1 -1
  144. package/dist/utils/typeGuards.d.ts +1 -1
  145. package/dist/utils/typeGuards.js +6 -2
  146. package/dist/utils/typeGuards.js.map +1 -1
  147. package/package.json +24 -43
package/dist/runState.js CHANGED
@@ -1,183 +1,227 @@
1
- import { z } from '@openai/zod/v3';
2
- import { Agent } from "./agent.js";
3
- import { RunMessageOutputItem, RunToolApprovalItem, RunToolCallItem, RunToolCallOutputItem, RunReasoningItem, RunHandoffCallItem, RunHandoffOutputItem, } from "./items.js";
4
- import { RunContext } from "./runContext.js";
5
- import { AgentToolUseTracker, nextStepSchema, } from "./runImplementation.js";
6
- import { SystemError, UserError } from "./errors.js";
7
- import { getGlobalTraceProvider } from "./tracing/provider.js";
8
- import { Usage } from "./usage.js";
9
- import { getCurrentTrace } from "./tracing/index.js";
10
- import logger from "./logger.js";
11
- import { handoff } from "./handoff.js";
12
- import * as protocol from "./types/protocol.js";
13
- import { safeExecute } from "./utils/safeExecute.js";
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.RunState = exports.SerializedRunState = exports.CURRENT_SCHEMA_VERSION = void 0;
40
+ exports.buildAgentMap = buildAgentMap;
41
+ exports.deserializeSpan = deserializeSpan;
42
+ exports.deserializeModelResponse = deserializeModelResponse;
43
+ exports.deserializeItem = deserializeItem;
44
+ const zod_1 = require("zod");
45
+ const agent_1 = require("./agent.js");
46
+ const items_1 = require("./items.js");
47
+ const runContext_1 = require("./runContext.js");
48
+ const run_1 = require("./run.js");
49
+ const runImplementation_1 = require("./runImplementation.js");
50
+ const errors_1 = require("./errors.js");
51
+ const provider_1 = require("./tracing/provider.js");
52
+ const usage_1 = require("./usage.js");
53
+ const tracing_1 = require("./tracing/index.js");
54
+ const logger_1 = __importDefault(require("./logger.js"));
55
+ const handoff_1 = require("./handoff.js");
56
+ const protocol = __importStar(require("./types/protocol.js"));
57
+ const safeExecute_1 = require("./utils/safeExecute.js");
14
58
  /**
15
59
  * The schema version of the serialized run state. This is used to ensure that the serialized
16
60
  * run state is compatible with the current version of the SDK.
17
61
  * If anything in this schema changes, the version will have to be incremented.
18
62
  */
19
- export const CURRENT_SCHEMA_VERSION = '1.0';
20
- const $schemaVersion = z.literal(CURRENT_SCHEMA_VERSION);
21
- const serializedAgentSchema = z.object({
22
- name: z.string(),
63
+ exports.CURRENT_SCHEMA_VERSION = '1.0';
64
+ const $schemaVersion = zod_1.z.literal(exports.CURRENT_SCHEMA_VERSION);
65
+ const serializedAgentSchema = zod_1.z.object({
66
+ name: zod_1.z.string(),
23
67
  });
24
- const serializedSpanBase = z.object({
25
- object: z.literal('trace.span'),
26
- id: z.string(),
27
- trace_id: z.string(),
28
- parent_id: z.string().nullable(),
29
- started_at: z.string().nullable(),
30
- ended_at: z.string().nullable(),
31
- error: z
68
+ const serializedSpanBase = zod_1.z.object({
69
+ object: zod_1.z.literal('trace.span'),
70
+ id: zod_1.z.string(),
71
+ trace_id: zod_1.z.string(),
72
+ parent_id: zod_1.z.string().nullable(),
73
+ started_at: zod_1.z.string().nullable(),
74
+ ended_at: zod_1.z.string().nullable(),
75
+ error: zod_1.z
32
76
  .object({
33
- message: z.string(),
34
- data: z.record(z.string(), z.any()).optional(),
77
+ message: zod_1.z.string(),
78
+ data: zod_1.z.record(zod_1.z.string(), zod_1.z.any()).optional(),
35
79
  })
36
80
  .nullable(),
37
- span_data: z.record(z.string(), z.any()),
81
+ span_data: zod_1.z.record(zod_1.z.string(), zod_1.z.any()),
38
82
  });
39
83
  const SerializedSpan = serializedSpanBase.extend({
40
- previous_span: z.lazy(() => SerializedSpan).optional(),
84
+ previous_span: zod_1.z.lazy(() => SerializedSpan).optional(),
41
85
  });
42
- const usageSchema = z.object({
43
- requests: z.number(),
44
- inputTokens: z.number(),
45
- outputTokens: z.number(),
46
- totalTokens: z.number(),
86
+ const usageSchema = zod_1.z.object({
87
+ requests: zod_1.z.number(),
88
+ inputTokens: zod_1.z.number(),
89
+ outputTokens: zod_1.z.number(),
90
+ totalTokens: zod_1.z.number(),
47
91
  });
48
- const modelResponseSchema = z.object({
92
+ const modelResponseSchema = zod_1.z.object({
49
93
  usage: usageSchema,
50
- output: z.array(protocol.OutputModelItem),
51
- responseId: z.string().optional(),
52
- providerData: z.record(z.string(), z.any()).optional(),
94
+ output: zod_1.z.array(protocol.OutputModelItem),
95
+ responseId: zod_1.z.string().optional(),
96
+ providerData: zod_1.z.record(zod_1.z.string(), zod_1.z.any()).optional(),
53
97
  });
54
- const itemSchema = z.discriminatedUnion('type', [
55
- z.object({
56
- type: z.literal('message_output_item'),
98
+ const itemSchema = zod_1.z.discriminatedUnion('type', [
99
+ zod_1.z.object({
100
+ type: zod_1.z.literal('message_output_item'),
57
101
  rawItem: protocol.AssistantMessageItem,
58
102
  agent: serializedAgentSchema,
59
103
  }),
60
- z.object({
61
- type: z.literal('tool_call_item'),
104
+ zod_1.z.object({
105
+ type: zod_1.z.literal('tool_call_item'),
62
106
  rawItem: protocol.ToolCallItem.or(protocol.HostedToolCallItem),
63
107
  agent: serializedAgentSchema,
64
108
  }),
65
- z.object({
66
- type: z.literal('tool_call_output_item'),
109
+ zod_1.z.object({
110
+ type: zod_1.z.literal('tool_call_output_item'),
67
111
  rawItem: protocol.FunctionCallResultItem,
68
112
  agent: serializedAgentSchema,
69
- output: z.string(),
113
+ output: zod_1.z.string(),
70
114
  }),
71
- z.object({
72
- type: z.literal('reasoning_item'),
115
+ zod_1.z.object({
116
+ type: zod_1.z.literal('reasoning_item'),
73
117
  rawItem: protocol.ReasoningItem,
74
118
  agent: serializedAgentSchema,
75
119
  }),
76
- z.object({
77
- type: z.literal('handoff_call_item'),
120
+ zod_1.z.object({
121
+ type: zod_1.z.literal('handoff_call_item'),
78
122
  rawItem: protocol.FunctionCallItem,
79
123
  agent: serializedAgentSchema,
80
124
  }),
81
- z.object({
82
- type: z.literal('handoff_output_item'),
125
+ zod_1.z.object({
126
+ type: zod_1.z.literal('handoff_output_item'),
83
127
  rawItem: protocol.FunctionCallResultItem,
84
128
  sourceAgent: serializedAgentSchema,
85
129
  targetAgent: serializedAgentSchema,
86
130
  }),
87
- z.object({
88
- type: z.literal('tool_approval_item'),
131
+ zod_1.z.object({
132
+ type: zod_1.z.literal('tool_approval_item'),
89
133
  rawItem: protocol.FunctionCallItem.or(protocol.HostedToolCallItem),
90
134
  agent: serializedAgentSchema,
91
135
  }),
92
136
  ]);
93
- const serializedTraceSchema = z.object({
94
- object: z.literal('trace'),
95
- id: z.string(),
96
- workflow_name: z.string(),
97
- group_id: z.string().nullable(),
98
- metadata: z.record(z.string(), z.any()),
137
+ const serializedTraceSchema = zod_1.z.object({
138
+ object: zod_1.z.literal('trace'),
139
+ id: zod_1.z.string(),
140
+ workflow_name: zod_1.z.string(),
141
+ group_id: zod_1.z.string().nullable(),
142
+ metadata: zod_1.z.record(zod_1.z.string(), zod_1.z.any()),
99
143
  });
100
- const serializedProcessedResponseSchema = z.object({
101
- newItems: z.array(itemSchema),
102
- toolsUsed: z.array(z.string()),
103
- handoffs: z.array(z.object({
104
- toolCall: z.any(),
105
- handoff: z.any(),
144
+ const serializedProcessedResponseSchema = zod_1.z.object({
145
+ newItems: zod_1.z.array(itemSchema),
146
+ toolsUsed: zod_1.z.array(zod_1.z.string()),
147
+ handoffs: zod_1.z.array(zod_1.z.object({
148
+ toolCall: zod_1.z.any(),
149
+ handoff: zod_1.z.any(),
106
150
  })),
107
- functions: z.array(z.object({
108
- toolCall: z.any(),
109
- tool: z.any(),
151
+ functions: zod_1.z.array(zod_1.z.object({
152
+ toolCall: zod_1.z.any(),
153
+ tool: zod_1.z.any(),
110
154
  })),
111
- computerActions: z.array(z.object({
112
- toolCall: z.any(),
113
- computer: z.any(),
155
+ computerActions: zod_1.z.array(zod_1.z.object({
156
+ toolCall: zod_1.z.any(),
157
+ computer: zod_1.z.any(),
114
158
  })),
115
- mcpApprovalRequests: z
116
- .array(z.object({
117
- requestItem: z.object({
159
+ mcpApprovalRequests: zod_1.z
160
+ .array(zod_1.z.object({
161
+ requestItem: zod_1.z.object({
118
162
  // protocol.HostedToolCallItem
119
- rawItem: z.object({
120
- type: z.literal('hosted_tool_call'),
121
- name: z.string(),
122
- arguments: z.string().optional(),
123
- status: z.string().optional(),
124
- output: z.string().optional(),
163
+ rawItem: zod_1.z.object({
164
+ type: zod_1.z.literal('hosted_tool_call'),
165
+ name: zod_1.z.string(),
166
+ arguments: zod_1.z.string().optional(),
167
+ status: zod_1.z.string().optional(),
168
+ output: zod_1.z.string().optional(),
125
169
  // this always exists but marked as optional for early version compatibility; when releasing 1.0, we can remove the nullable and optional
126
- providerData: z.record(z.string(), z.any()).nullable().optional(),
170
+ providerData: zod_1.z.record(zod_1.z.string(), zod_1.z.any()).nullable().optional(),
127
171
  }),
128
172
  }),
129
173
  // HostedMCPTool
130
- mcpTool: z.object({
131
- type: z.literal('hosted_tool'),
132
- name: z.literal('hosted_mcp'),
133
- providerData: z.record(z.string(), z.any()),
174
+ mcpTool: zod_1.z.object({
175
+ type: zod_1.z.literal('hosted_tool'),
176
+ name: zod_1.z.literal('hosted_mcp'),
177
+ providerData: zod_1.z.record(zod_1.z.string(), zod_1.z.any()),
134
178
  }),
135
179
  }))
136
180
  .optional(),
137
181
  });
138
- const guardrailFunctionOutputSchema = z.object({
139
- tripwireTriggered: z.boolean(),
140
- outputInfo: z.any(),
182
+ const guardrailFunctionOutputSchema = zod_1.z.object({
183
+ tripwireTriggered: zod_1.z.boolean(),
184
+ outputInfo: zod_1.z.any(),
141
185
  });
142
- const inputGuardrailResultSchema = z.object({
143
- guardrail: z.object({
144
- type: z.literal('input'),
145
- name: z.string(),
186
+ const inputGuardrailResultSchema = zod_1.z.object({
187
+ guardrail: zod_1.z.object({
188
+ type: zod_1.z.literal('input'),
189
+ name: zod_1.z.string(),
146
190
  }),
147
191
  output: guardrailFunctionOutputSchema,
148
192
  });
149
- const outputGuardrailResultSchema = z.object({
150
- guardrail: z.object({
151
- type: z.literal('output'),
152
- name: z.string(),
193
+ const outputGuardrailResultSchema = zod_1.z.object({
194
+ guardrail: zod_1.z.object({
195
+ type: zod_1.z.literal('output'),
196
+ name: zod_1.z.string(),
153
197
  }),
154
- agentOutput: z.any(),
198
+ agentOutput: zod_1.z.any(),
155
199
  agent: serializedAgentSchema,
156
200
  output: guardrailFunctionOutputSchema,
157
201
  });
158
- export const SerializedRunState = z.object({
202
+ exports.SerializedRunState = zod_1.z.object({
159
203
  $schemaVersion,
160
- currentTurn: z.number(),
204
+ currentTurn: zod_1.z.number(),
161
205
  currentAgent: serializedAgentSchema,
162
- originalInput: z.string().or(z.array(protocol.ModelItem)),
163
- modelResponses: z.array(modelResponseSchema),
164
- context: z.object({
206
+ originalInput: zod_1.z.string().or(zod_1.z.array(protocol.ModelItem)),
207
+ modelResponses: zod_1.z.array(modelResponseSchema),
208
+ context: zod_1.z.object({
165
209
  usage: usageSchema,
166
- approvals: z.record(z.string(), z.object({
167
- approved: z.array(z.string()).or(z.boolean()),
168
- rejected: z.array(z.string()).or(z.boolean()),
210
+ approvals: zod_1.z.record(zod_1.z.string(), zod_1.z.object({
211
+ approved: zod_1.z.array(zod_1.z.string()).or(zod_1.z.boolean()),
212
+ rejected: zod_1.z.array(zod_1.z.string()).or(zod_1.z.boolean()),
169
213
  })),
170
- context: z.record(z.string(), z.any()),
214
+ context: zod_1.z.record(zod_1.z.string(), zod_1.z.any()),
171
215
  }),
172
- toolUseTracker: z.record(z.string(), z.array(z.string())),
173
- maxTurns: z.number(),
216
+ toolUseTracker: zod_1.z.record(zod_1.z.string(), zod_1.z.array(zod_1.z.string())),
217
+ maxTurns: zod_1.z.number(),
174
218
  currentAgentSpan: SerializedSpan.nullable().optional(),
175
- noActiveAgentRun: z.boolean(),
176
- inputGuardrailResults: z.array(inputGuardrailResultSchema),
177
- outputGuardrailResults: z.array(outputGuardrailResultSchema),
178
- currentStep: nextStepSchema.optional(),
219
+ noActiveAgentRun: zod_1.z.boolean(),
220
+ inputGuardrailResults: zod_1.z.array(inputGuardrailResultSchema),
221
+ outputGuardrailResults: zod_1.z.array(outputGuardrailResultSchema),
222
+ currentStep: runImplementation_1.nextStepSchema.optional(),
179
223
  lastModelResponse: modelResponseSchema.optional(),
180
- generatedItems: z.array(itemSchema),
224
+ generatedItems: zod_1.z.array(itemSchema),
181
225
  lastProcessedResponse: serializedProcessedResponseSchema.optional(),
182
226
  trace: serializedTraceSchema.nullable(),
183
227
  });
@@ -189,7 +233,7 @@ export const SerializedRunState = z.object({
189
233
  * Manipulation of the state directly can lead to unexpected behavior and should be avoided.
190
234
  * Instead, use the `approve` and `reject` methods to interact with the state.
191
235
  */
192
- export class RunState {
236
+ class RunState {
193
237
  /**
194
238
  * Current turn number in the conversation.
195
239
  */
@@ -260,12 +304,20 @@ export class RunState {
260
304
  this._modelResponses = [];
261
305
  this._currentAgentSpan = undefined;
262
306
  this._currentAgent = startingAgent;
263
- this._toolUseTracker = new AgentToolUseTracker();
307
+ this._toolUseTracker = new runImplementation_1.AgentToolUseTracker();
264
308
  this._generatedItems = [];
265
309
  this._maxTurns = maxTurns;
266
310
  this._inputGuardrailResults = [];
267
311
  this._outputGuardrailResults = [];
268
- this._trace = getCurrentTrace();
312
+ this._trace = (0, tracing_1.getCurrentTrace)();
313
+ }
314
+ /**
315
+ * The history of the agent run. This includes the input items and the new items generated during the run.
316
+ *
317
+ * This can be used as inputs for the next agent run.
318
+ */
319
+ get history() {
320
+ return (0, run_1.getTurnInput)(this._originalInput, this._generatedItems);
269
321
  }
270
322
  /**
271
323
  * Returns all interruptions if the current step is an interruption otherwise returns an empty array.
@@ -316,7 +368,7 @@ export class RunState {
316
368
  */
317
369
  toJSON() {
318
370
  const output = {
319
- $schemaVersion: CURRENT_SCHEMA_VERSION,
371
+ $schemaVersion: exports.CURRENT_SCHEMA_VERSION,
320
372
  currentTurn: this._currentTurn,
321
373
  currentAgent: {
322
374
  name: this._currentAgent.name,
@@ -352,9 +404,9 @@ export class RunState {
352
404
  trace: this._trace ? this._trace.toJSON() : null,
353
405
  };
354
406
  // parsing the schema to ensure the output is valid for reparsing
355
- const parsed = SerializedRunState.safeParse(output);
407
+ const parsed = exports.SerializedRunState.safeParse(output);
356
408
  if (!parsed.success) {
357
- throw new SystemError(`Failed to serialize run state. ${parsed.error.message}`);
409
+ throw new errors_1.SystemError(`Failed to serialize run state. ${parsed.error.message}`);
358
410
  }
359
411
  return parsed.data;
360
412
  }
@@ -376,44 +428,44 @@ export class RunState {
376
428
  * `toString` method.
377
429
  */
378
430
  static async fromString(initialAgent, str) {
379
- const [parsingError, jsonResult] = await safeExecute(() => JSON.parse(str));
431
+ const [parsingError, jsonResult] = await (0, safeExecute_1.safeExecute)(() => JSON.parse(str));
380
432
  if (parsingError) {
381
- throw new UserError(`Failed to parse run state. ${parsingError instanceof Error ? parsingError.message : String(parsingError)}`);
433
+ throw new errors_1.UserError(`Failed to parse run state. ${parsingError instanceof Error ? parsingError.message : String(parsingError)}`);
382
434
  }
383
435
  const currentSchemaVersion = jsonResult.$schemaVersion;
384
436
  if (!currentSchemaVersion) {
385
- throw new UserError('Run state is missing schema version');
437
+ throw new errors_1.UserError('Run state is missing schema version');
386
438
  }
387
- if (currentSchemaVersion !== CURRENT_SCHEMA_VERSION) {
388
- throw new UserError(`Run state schema version ${currentSchemaVersion} is not supported. Please use version ${CURRENT_SCHEMA_VERSION}`);
439
+ if (currentSchemaVersion !== exports.CURRENT_SCHEMA_VERSION) {
440
+ throw new errors_1.UserError(`Run state schema version ${currentSchemaVersion} is not supported. Please use version ${exports.CURRENT_SCHEMA_VERSION}`);
389
441
  }
390
- const stateJson = SerializedRunState.parse(JSON.parse(str));
442
+ const stateJson = exports.SerializedRunState.parse(JSON.parse(str));
391
443
  const agentMap = buildAgentMap(initialAgent);
392
444
  //
393
445
  // Rebuild the context
394
446
  //
395
- const context = new RunContext(stateJson.context.context);
447
+ const context = new runContext_1.RunContext(stateJson.context.context);
396
448
  context._rebuildApprovals(stateJson.context.approvals);
397
449
  //
398
450
  // Find the current agent from the initial agent
399
451
  //
400
452
  const currentAgent = agentMap.get(stateJson.currentAgent.name);
401
453
  if (!currentAgent) {
402
- throw new UserError(`Agent ${stateJson.currentAgent.name} not found`);
454
+ throw new errors_1.UserError(`Agent ${stateJson.currentAgent.name} not found`);
403
455
  }
404
456
  const state = new RunState(context, '', currentAgent, stateJson.maxTurns);
405
457
  state._currentTurn = stateJson.currentTurn;
406
458
  // rebuild tool use tracker
407
- state._toolUseTracker = new AgentToolUseTracker();
459
+ state._toolUseTracker = new runImplementation_1.AgentToolUseTracker();
408
460
  for (const [agentName, toolNames] of Object.entries(stateJson.toolUseTracker)) {
409
461
  state._toolUseTracker.addToolUse(agentMap.get(agentName), toolNames);
410
462
  }
411
463
  // rebuild current agent span
412
464
  if (stateJson.currentAgentSpan) {
413
465
  if (!stateJson.trace) {
414
- logger.warn('Trace is not set, skipping tracing setup');
466
+ logger_1.default.warn('Trace is not set, skipping tracing setup');
415
467
  }
416
- const trace = getGlobalTraceProvider().createTrace({
468
+ const trace = (0, provider_1.getGlobalTraceProvider)().createTrace({
417
469
  traceId: stateJson.trace?.id,
418
470
  name: stateJson.trace?.workflow_name,
419
471
  groupId: stateJson.trace?.group_id ?? undefined,
@@ -448,10 +500,11 @@ export class RunState {
448
500
  return state;
449
501
  }
450
502
  }
503
+ exports.RunState = RunState;
451
504
  /**
452
505
  * @internal
453
506
  */
454
- export function buildAgentMap(initialAgent) {
507
+ function buildAgentMap(initialAgent) {
455
508
  const map = new Map();
456
509
  const queue = [initialAgent];
457
510
  while (queue.length > 0) {
@@ -461,7 +514,7 @@ export function buildAgentMap(initialAgent) {
461
514
  }
462
515
  map.set(currentAgent.name, currentAgent);
463
516
  for (const handoff of currentAgent.handoffs) {
464
- if (handoff instanceof Agent) {
517
+ if (handoff instanceof agent_1.Agent) {
465
518
  if (!map.has(handoff.name)) {
466
519
  queue.push(handoff);
467
520
  }
@@ -478,12 +531,12 @@ export function buildAgentMap(initialAgent) {
478
531
  /**
479
532
  * @internal
480
533
  */
481
- export function deserializeSpan(trace, serializedSpan) {
534
+ function deserializeSpan(trace, serializedSpan) {
482
535
  const spanData = serializedSpan.span_data;
483
536
  const previousSpan = serializedSpan.previous_span
484
537
  ? deserializeSpan(trace, serializedSpan.previous_span)
485
538
  : undefined;
486
- const span = getGlobalTraceProvider().createSpan({
539
+ const span = (0, provider_1.getGlobalTraceProvider)().createSpan({
487
540
  spanId: serializedSpan.id,
488
541
  traceId: serializedSpan.trace_id,
489
542
  parentId: serializedSpan.parent_id ?? undefined,
@@ -497,8 +550,8 @@ export function deserializeSpan(trace, serializedSpan) {
497
550
  /**
498
551
  * @internal
499
552
  */
500
- export function deserializeModelResponse(serializedModelResponse) {
501
- const usage = new Usage();
553
+ function deserializeModelResponse(serializedModelResponse) {
554
+ const usage = new usage_1.Usage();
502
555
  usage.requests = serializedModelResponse.usage.requests;
503
556
  usage.inputTokens = serializedModelResponse.usage.inputTokens;
504
557
  usage.outputTokens = serializedModelResponse.usage.outputTokens;
@@ -513,22 +566,22 @@ export function deserializeModelResponse(serializedModelResponse) {
513
566
  /**
514
567
  * @internal
515
568
  */
516
- export function deserializeItem(serializedItem, agentMap) {
569
+ function deserializeItem(serializedItem, agentMap) {
517
570
  switch (serializedItem.type) {
518
571
  case 'message_output_item':
519
- return new RunMessageOutputItem(serializedItem.rawItem, agentMap.get(serializedItem.agent.name));
572
+ return new items_1.RunMessageOutputItem(serializedItem.rawItem, agentMap.get(serializedItem.agent.name));
520
573
  case 'tool_call_item':
521
- return new RunToolCallItem(serializedItem.rawItem, agentMap.get(serializedItem.agent.name));
574
+ return new items_1.RunToolCallItem(serializedItem.rawItem, agentMap.get(serializedItem.agent.name));
522
575
  case 'tool_call_output_item':
523
- return new RunToolCallOutputItem(serializedItem.rawItem, agentMap.get(serializedItem.agent.name), serializedItem.output);
576
+ return new items_1.RunToolCallOutputItem(serializedItem.rawItem, agentMap.get(serializedItem.agent.name), serializedItem.output);
524
577
  case 'reasoning_item':
525
- return new RunReasoningItem(serializedItem.rawItem, agentMap.get(serializedItem.agent.name));
578
+ return new items_1.RunReasoningItem(serializedItem.rawItem, agentMap.get(serializedItem.agent.name));
526
579
  case 'handoff_call_item':
527
- return new RunHandoffCallItem(serializedItem.rawItem, agentMap.get(serializedItem.agent.name));
580
+ return new items_1.RunHandoffCallItem(serializedItem.rawItem, agentMap.get(serializedItem.agent.name));
528
581
  case 'handoff_output_item':
529
- return new RunHandoffOutputItem(serializedItem.rawItem, agentMap.get(serializedItem.sourceAgent.name), agentMap.get(serializedItem.targetAgent.name));
582
+ return new items_1.RunHandoffOutputItem(serializedItem.rawItem, agentMap.get(serializedItem.sourceAgent.name), agentMap.get(serializedItem.targetAgent.name));
530
583
  case 'tool_approval_item':
531
- return new RunToolApprovalItem(serializedItem.rawItem, agentMap.get(serializedItem.agent.name));
584
+ return new items_1.RunToolApprovalItem(serializedItem.rawItem, agentMap.get(serializedItem.agent.name));
532
585
  }
533
586
  }
534
587
  /**
@@ -543,8 +596,8 @@ async function deserializeProcessedResponse(agentMap, currentAgent, context, ser
543
596
  .filter((tool) => tool.type === 'computer')
544
597
  .map((tool) => [tool.name, tool]));
545
598
  const handoffs = new Map(currentAgent.handoffs.map((entry) => {
546
- if (entry instanceof Agent) {
547
- return [entry.name, handoff(entry)];
599
+ if (entry instanceof agent_1.Agent) {
600
+ return [entry.name, (0, handoff_1.handoff)(entry)];
548
601
  }
549
602
  return [entry.toolName, entry];
550
603
  }));
@@ -553,7 +606,7 @@ async function deserializeProcessedResponse(agentMap, currentAgent, context, ser
553
606
  toolsUsed: serializedProcessedResponse.toolsUsed,
554
607
  handoffs: serializedProcessedResponse.handoffs.map((handoff) => {
555
608
  if (!handoffs.has(handoff.handoff.toolName)) {
556
- throw new UserError(`Handoff ${handoff.handoff.toolName} not found`);
609
+ throw new errors_1.UserError(`Handoff ${handoff.handoff.toolName} not found`);
557
610
  }
558
611
  return {
559
612
  toolCall: handoff.toolCall,
@@ -562,7 +615,7 @@ async function deserializeProcessedResponse(agentMap, currentAgent, context, ser
562
615
  }),
563
616
  functions: await Promise.all(serializedProcessedResponse.functions.map(async (functionCall) => {
564
617
  if (!tools.has(functionCall.tool.name)) {
565
- throw new UserError(`Tool ${functionCall.tool.name} not found`);
618
+ throw new errors_1.UserError(`Tool ${functionCall.tool.name} not found`);
566
619
  }
567
620
  return {
568
621
  toolCall: functionCall.toolCall,
@@ -572,7 +625,7 @@ async function deserializeProcessedResponse(agentMap, currentAgent, context, ser
572
625
  computerActions: serializedProcessedResponse.computerActions.map((computerAction) => {
573
626
  const toolName = computerAction.computer.name;
574
627
  if (!computerTools.has(toolName)) {
575
- throw new UserError(`Computer tool ${toolName} not found`);
628
+ throw new errors_1.UserError(`Computer tool ${toolName} not found`);
576
629
  }
577
630
  return {
578
631
  toolCall: computerAction.toolCall,
@@ -580,7 +633,7 @@ async function deserializeProcessedResponse(agentMap, currentAgent, context, ser
580
633
  };
581
634
  }),
582
635
  mcpApprovalRequests: (serializedProcessedResponse.mcpApprovalRequests ?? []).map((approvalRequest) => ({
583
- requestItem: new RunToolApprovalItem(approvalRequest.requestItem
636
+ requestItem: new items_1.RunToolApprovalItem(approvalRequest.requestItem
584
637
  .rawItem, currentAgent),
585
638
  mcpTool: approvalRequest.mcpTool,
586
639
  })),