Skip to content

feat(agents-core): expose RunState history getter #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/expose-runstate-history.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@openai/agents-core': patch
---

feat: expose the `history` getter on `RunState` to access input and generated items.
10 changes: 10 additions & 0 deletions packages/agents-core/src/runState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from './items';
import type { ModelResponse } from './model';
import { RunContext } from './runContext';
import { getTurnInput } from './run';
import {
AgentToolUseTracker,
nextStepSchema,
Expand Down Expand Up @@ -327,6 +328,15 @@ export class RunState<TContext, TAgent extends Agent<any, any>> {
this._trace = getCurrentTrace();
}

/**
* The history of the agent run. This includes the input items and the new items generated during the run.
*
* This can be used as inputs for the next agent run.
*/
get history(): AgentInputItem[] {
return getTurnInput(this._originalInput, this._generatedItems);
}

/**
* Returns all interruptions if the current step is an interruption otherwise returns an empty array.
*/
Expand Down
31 changes: 30 additions & 1 deletion packages/agents-core/test/runState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
} from '../src/runState';
import { RunContext } from '../src/runContext';
import { Agent } from '../src/agent';
import { RunToolApprovalItem as ToolApprovalItem } from '../src/items';
import {
RunToolApprovalItem as ToolApprovalItem,
RunMessageOutputItem,
} from '../src/items';
import { computerTool } from '../src/tool';
import * as protocol from '../src/types/protocol';
import { TEST_MODEL_MESSAGE, FakeComputer } from './stubs';
Expand All @@ -31,6 +34,32 @@ describe('RunState', () => {
expect(state._context.context).toEqual({ foo: 'bar' });
});

it('returns history including original input and generated items', () => {
const context = new RunContext();
const agent = new Agent({ name: 'HistAgent' });
const state = new RunState(context, 'input', agent, 1);
state._generatedItems.push(
new RunMessageOutputItem(TEST_MODEL_MESSAGE, agent),
);

expect(state.history).toEqual([
{ type: 'message', role: 'user', content: 'input' },
TEST_MODEL_MESSAGE,
]);
});

it('preserves history after serialization', async () => {
const context = new RunContext();
const agent = new Agent({ name: 'HistAgent2' });
const state = new RunState(context, 'input', agent, 1);
state._generatedItems.push(
new RunMessageOutputItem(TEST_MODEL_MESSAGE, agent),
);

const restored = await RunState.fromString(agent, state.toString());
expect(restored.history).toEqual(state.history);
});

it('toJSON and toString produce valid JSON', () => {
const context = new RunContext();
const agent = new Agent({ name: 'Agent1' });
Expand Down