Skip to content
Open
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
70 changes: 70 additions & 0 deletions src/lib/responses/ResponseInputItems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type {
ResponseFunctionShellCallOutputContent,
ResponseInputItem,
ResponseOutputItem,
} from '../../resources/responses/responses';

export type ResponseInputItemLike = ResponseInputItem | ResponseOutputItem;

type ResponseShellCallOutputInputItem = Extract<ResponseInputItem, { type: 'shell_call_output' }>;

/**
* Normalizes a mixed array of stored response history items into clean
* `ResponseInputItem`s that can be sent back to `responses.create()`.
*/
export function toResponseInputItems(items: Iterable<ResponseInputItemLike>): ResponseInputItem[] {
return Array.from(items, toResponseInputItem);
}

/**
* Normalizes a stored response history item into a clean `ResponseInputItem`.
*/
export function toResponseInputItem(item: ResponseInputItemLike): ResponseInputItem {
switch (item.type) {
case 'apply_patch_call': {
return stripCreatedBy(item);
}

case 'apply_patch_call_output': {
return stripCreatedBy(item);
}

case 'compaction': {
return stripCreatedBy(item);
}

case 'shell_call': {
return stripCreatedBy(item);
}

case 'shell_call_output': {
const output: ResponseShellCallOutputInputItem['output'] = item.output.map(
(chunk) => stripCreatedBy(chunk) as ResponseFunctionShellCallOutputContent,
);
return {
...(stripCreatedBy(item) as ResponseShellCallOutputInputItem),
output,
};
}

case 'tool_search_call': {
return stripCreatedBy(item);
}

case 'tool_search_output': {
return stripCreatedBy(item);
}

default:
return item;
}
}

function stripCreatedBy<T extends object>(item: T): T {
if (!('created_by' in item)) {
return item;
}

const { created_by: _createdBy, ...rest } = item as T & { created_by?: string };
return rest as T;
}
143 changes: 143 additions & 0 deletions tests/lib/ResponseInputItems.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { toResponseInputItems } from 'openai/lib/responses/ResponseInputItems';
import type { ResponseInputItem, ResponseOutputItem } from 'openai/resources/responses';

describe('toResponseInputItems', () => {
test('normalizes mixed response history items', () => {
const history: Array<ResponseInputItem | ResponseOutputItem> = [
{
type: 'function_call_output',
call_id: 'function_call_123',
output: 'done',
},
{
type: 'tool_search_call',
id: 'tool_search_item_123',
call_id: 'tool_search_call_123',
arguments: { query: 'schema' },
execution: 'server',
status: 'completed',
created_by: 'assistant',
},
{
type: 'compaction',
id: 'compaction_123',
encrypted_content: 'encrypted',
created_by: 'assistant',
},
{
type: 'shell_call',
id: 'shell_call_123',
call_id: 'shell_call_123',
action: {
commands: ['pwd'],
max_output_length: 512,
timeout_ms: 1000,
},
environment: null,
status: 'completed',
created_by: 'assistant',
},
{
type: 'shell_call_output',
id: 'shell_call_output_123',
call_id: 'shell_call_123',
max_output_length: 512,
output: [
{
stdout: '/workspace\n',
stderr: '',
outcome: { type: 'exit', exit_code: 0 },
created_by: 'assistant',
},
],
status: 'completed',
created_by: 'assistant',
},
{
type: 'apply_patch_call',
id: 'apply_patch_call_123',
call_id: 'apply_patch_call_123',
operation: {
type: 'create_file',
path: 'notes.txt',
diff: 'hello',
},
status: 'completed',
created_by: 'assistant',
},
{
type: 'apply_patch_call_output',
id: 'apply_patch_call_output_123',
call_id: 'apply_patch_call_123',
output: 'created notes.txt',
status: 'completed',
created_by: 'assistant',
},
];

expect(toResponseInputItems(history)).toEqual([
{
type: 'function_call_output',
call_id: 'function_call_123',
output: 'done',
},
{
type: 'tool_search_call',
id: 'tool_search_item_123',
call_id: 'tool_search_call_123',
arguments: { query: 'schema' },
execution: 'server',
status: 'completed',
},
{
type: 'compaction',
id: 'compaction_123',
encrypted_content: 'encrypted',
},
{
type: 'shell_call',
id: 'shell_call_123',
call_id: 'shell_call_123',
action: {
commands: ['pwd'],
max_output_length: 512,
timeout_ms: 1000,
},
environment: null,
status: 'completed',
},
{
type: 'shell_call_output',
id: 'shell_call_output_123',
call_id: 'shell_call_123',
max_output_length: 512,
output: [
{
stdout: '/workspace\n',
stderr: '',
outcome: { type: 'exit', exit_code: 0 },
},
],
status: 'completed',
},
{
type: 'apply_patch_call',
id: 'apply_patch_call_123',
call_id: 'apply_patch_call_123',
operation: {
type: 'create_file',
path: 'notes.txt',
diff: 'hello',
},
status: 'completed',
},
{
type: 'apply_patch_call_output',
id: 'apply_patch_call_output_123',
call_id: 'apply_patch_call_123',
output: 'created notes.txt',
status: 'completed',
},
]);
});
});
18 changes: 18 additions & 0 deletions tests/responsesItems.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import OpenAI from 'openai/index';
import { toResponseInputItems } from 'openai/lib/responses/ResponseInputItems';
import type { ResponseInputItem, ResponseOutputItem } from 'openai/resources/responses';

const openai = new OpenAI({ apiKey: 'example-api-key' });

describe('responses item types', () => {
Expand All @@ -12,10 +15,25 @@ const unused = async () => {
model: 'gpt-5.1',
input: 'You are a helpful assistant.',
});

const history: Array<ResponseInputItem | ResponseOutputItem> = [
{
type: 'function_call_output',
call_id: 'call_123',
output: 'done',
},
...response.output,
];

await openai.responses.create({
model: 'gpt-5.1',
// check type compatibility
input: response.output,
});
await openai.responses.create({
model: 'gpt-5.1',
// check mixed history normalization
input: toResponseInputItems(history),
});
expect(true).toBe(true);
};