Skip to content

Commit 1cd3266

Browse files
feat(agents-core): expose RunState history getter (#340)
1 parent f825f71 commit 1cd3266

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

.changeset/expose-runstate-history.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openai/agents-core': patch
3+
---
4+
5+
feat: expose the `history` getter on `RunState` to access input and generated items.

packages/agents-core/src/runState.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from './items';
1313
import type { ModelResponse } from './model';
1414
import { RunContext } from './runContext';
15+
import { getTurnInput } from './run';
1516
import {
1617
AgentToolUseTracker,
1718
nextStepSchema,
@@ -327,6 +328,15 @@ export class RunState<TContext, TAgent extends Agent<any, any>> {
327328
this._trace = getCurrentTrace();
328329
}
329330

331+
/**
332+
* The history of the agent run. This includes the input items and the new items generated during the run.
333+
*
334+
* This can be used as inputs for the next agent run.
335+
*/
336+
get history(): AgentInputItem[] {
337+
return getTurnInput(this._originalInput, this._generatedItems);
338+
}
339+
330340
/**
331341
* Returns all interruptions if the current step is an interruption otherwise returns an empty array.
332342
*/

packages/agents-core/test/runState.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import {
88
} from '../src/runState';
99
import { RunContext } from '../src/runContext';
1010
import { Agent } from '../src/agent';
11-
import { RunToolApprovalItem as ToolApprovalItem } from '../src/items';
11+
import {
12+
RunToolApprovalItem as ToolApprovalItem,
13+
RunMessageOutputItem,
14+
} from '../src/items';
1215
import { computerTool } from '../src/tool';
1316
import * as protocol from '../src/types/protocol';
1417
import { TEST_MODEL_MESSAGE, FakeComputer } from './stubs';
@@ -31,6 +34,32 @@ describe('RunState', () => {
3134
expect(state._context.context).toEqual({ foo: 'bar' });
3235
});
3336

37+
it('returns history including original input and generated items', () => {
38+
const context = new RunContext();
39+
const agent = new Agent({ name: 'HistAgent' });
40+
const state = new RunState(context, 'input', agent, 1);
41+
state._generatedItems.push(
42+
new RunMessageOutputItem(TEST_MODEL_MESSAGE, agent),
43+
);
44+
45+
expect(state.history).toEqual([
46+
{ type: 'message', role: 'user', content: 'input' },
47+
TEST_MODEL_MESSAGE,
48+
]);
49+
});
50+
51+
it('preserves history after serialization', async () => {
52+
const context = new RunContext();
53+
const agent = new Agent({ name: 'HistAgent2' });
54+
const state = new RunState(context, 'input', agent, 1);
55+
state._generatedItems.push(
56+
new RunMessageOutputItem(TEST_MODEL_MESSAGE, agent),
57+
);
58+
59+
const restored = await RunState.fromString(agent, state.toString());
60+
expect(restored.history).toEqual(state.history);
61+
});
62+
3463
it('toJSON and toString produce valid JSON', () => {
3564
const context = new RunContext();
3665
const agent = new Agent({ name: 'Agent1' });

0 commit comments

Comments
 (0)