Skip to content
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions packages/protocols/__tests__/openai-responses/history_test.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions packages/protocols/src/openai-chat-completions/history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { OpenAIChatCompletionsAssistantMessageEx, OpenAIChatCompletionsAssistantOutputMessageEx } from './index.ts';

export const toOpenAIChatCompletionsInputAssistantMessage = (outputTurn: OpenAIChatCompletionsAssistantOutputMessageEx): OpenAIChatCompletionsAssistantMessageEx => {
// The SDK serializes supplied message fields directly; prior audio input uses an ID reference.
// https://github.com/openai/openai-node/blob/f57c9651c8d22ae82cacca27317cfa7126491011/src/resources/chat/completions/completions.ts#L195-L210
// https://github.com/openai/openai-node/blob/f57c9651c8d22ae82cacca27317cfa7126491011/src/client.ts#L637-L637
// https://github.com/openai/openai-node/blob/f57c9651c8d22ae82cacca27317cfa7126491011/src/internal/request-options.ts#L115-L144
// The response object also carries audio bytes, expiry, and transcript.
// https://github.com/openai/openai-node/blob/f57c9651c8d22ae82cacca27317cfa7126491011/src/resources/chat/completions/completions.ts#L884-L935
// https://github.com/openai/openai-node/blob/f57c9651c8d22ae82cacca27317cfa7126491011/src/resources/chat/completions/completions.ts#L962-L984
const { audio, ...message } = outputTurn;
return {
...message,
...(Object.hasOwn(outputTurn, 'audio') ? { audio: audio === null || audio === undefined ? audio : { id: audio.id } } : {}),
};
};
2 changes: 2 additions & 0 deletions packages/protocols/src/openai-chat-completions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,5 @@ export { parseOpenAIChatCompletionsStream, type ParseOpenAIChatCompletionsStream
export { collectOpenAIChatCompletionsProtocolEventsToResult } from './to-result.ts';
export { createOpenAIChatCompletionsEventAccumulator, reassembleOpenAIChatCompletionsEvents, type OpenAIChatCompletionsEventAccumulator } from './reassemble.ts';
export { openaiChatCompletionsProtocolFrameToSSEFrame } from './to-sse.ts';

export { toOpenAIChatCompletionsInputAssistantMessage } from './history.ts';
52 changes: 52 additions & 0 deletions packages/protocols/src/openai-responses/history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { CanonicalOpenAIResponsesInputItem, OpenAIResponsesOutputItemEx } from './index.ts';

// Match the official SDK's history normalization for SDK-owned fields and replay-only item transforms.
// https://github.com/openai/openai-node/blob/f57c9651c8d22ae82cacca27317cfa7126491011/src/lib/responses/ResponseInputItems.ts#L41-L67
// https://github.com/openai/openai-node/blob/f57c9651c8d22ae82cacca27317cfa7126491011/src/lib/responses/ResponseInputItems.ts#L115-L160
type OpenAIResponsesOutputMessage = Extract<OpenAIResponsesOutputItemEx, { type: 'message' }>;
type OpenAIResponsesOutputContent = OpenAIResponsesOutputMessage['content'][number];

const stripCreatedBy = (item: OpenAIResponsesOutputItemEx): OpenAIResponsesOutputItemEx => {
if (!('created_by' in item)) return item;
const { created_by: _createdBy, ...historyItem } = item;
return historyItem;
};

const outputContentToHistoryContent = (part: OpenAIResponsesOutputContent): OpenAIResponsesOutputContent => {
if ((part.type !== 'output_text' && part.type !== 'refusal') || !('parsed' in part)) return part;
const { parsed: _parsed, ...historyContent } = part;
return historyContent;
};

// OpenAI defines output and replayable input items as separate unions. After the
// SDK's documented output-only fields and variants are removed below, the wire
// object is an input item even though TypeScript cannot encode that union relation.
const asInputItem = (item: OpenAIResponsesOutputItemEx): CanonicalOpenAIResponsesInputItem => item as CanonicalOpenAIResponsesInputItem;

export const toOpenAIResponsesInputItem = (item: OpenAIResponsesOutputItemEx): CanonicalOpenAIResponsesInputItem | null => {
const historyItem = stripCreatedBy(item);
if (historyItem.type === 'message') {
const content = historyItem.content.map(part => outputContentToHistoryContent(part));
return asInputItem(content.every((part, index) => part === historyItem.content[index]) ? historyItem : { ...historyItem, content });
}
if (historyItem.type === 'function_call') {
if (!('parsed_arguments' in historyItem)) return asInputItem(historyItem);
const { parsed_arguments: _parsedArguments, ...withoutParsedArguments } = historyItem;
return asInputItem(withoutParsedArguments);
}
if (historyItem.type === 'shell_call_output') {
return asInputItem({ ...historyItem, output: historyItem.output.map(({ created_by: _createdBy, ...output }) => output) });
}
if (historyItem.type === 'computer_call_output') return historyItem.status === 'failed' ? null : asInputItem(historyItem);
if (historyItem.type === 'custom_tool_call_output') {
if ('status' in historyItem && historyItem.status !== 'completed') return null;
const { status: _status, ...inputItem } = historyItem;
return asInputItem(inputItem);
}
return asInputItem(historyItem);
};

export const toOpenAIResponsesInputItems = (output: readonly OpenAIResponsesOutputItemEx[]): CanonicalOpenAIResponsesInputItem[] => output.flatMap(item => {
const input = toOpenAIResponsesInputItem(item);
return input === null ? [] : [input];
});
1 change: 1 addition & 0 deletions packages/protocols/src/openai-responses/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1450,3 +1450,4 @@ export { reassembleOpenAIResponsesEvents } from './reassemble.ts';
export { openaiResponsesProtocolFrameToSSEFrame } from './to-sse.ts';

export { canonicalizeOpenAIResponsesPayload } from './canonicalize-payload.ts';
export { toOpenAIResponsesInputItem, toOpenAIResponsesInputItems } from './history.ts';
3 changes: 2 additions & 1 deletion packages/translate/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ Protocol-native request normalization and Part data classification belong to
6. **Assistant-turn round trips** — keep owner-specific sidecar validation, replay
validation, and native stream coordination under `round-trip/`. Source replay
inspection and carriers belong in `as-source/round-trip.ts`; native assistant-turn
shapes and output-to-input converters belong in `as-target/assistant-turn.ts`.
reference shapes and state alignment belong in `as-target/assistant-turn.ts`.
Native output-to-input payload normalization belongs in `@floway-dev/protocols`.
Transport encoding is injected through `TranslateOptions.sidecarCodec`.
7. **Protocol-independent utility** — keep it in `shared/`, with tests under
`__tests__/shared/`. Protocol behavior belongs in its owning protocol directory.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading