You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR preserves the Responses API phase parameter ("commentary" | "final_answer") across round-trips by extracting it from response output items into the internal AssistantMessage representation and re-injecting it when converting messages back to API input items.
Key changes:
openai-ws-connection.ts — Introduces the OpenAIResponsesAssistantPhase type and adds the optional phase field to both OutputItem (message variant) and InputItem (message variant).
openai-ws-stream.ts — buildAssistantMessageFromResponse collects the phase from output items and attaches it to the produced AssistantMessage via a spread; convertMessagesToInputItems reads that phase back and includes it on every generated assistant message input item.
openai-ws-stream.test.ts — Adds targeted tests for all four affected paths (plain assistant message, assistant message with tool call, buildAssistantMessageFromResponse, and the full WebSocket stream path).
Minor style observations:
The InputItem message type union allows phase on all roles (system, developer, user, assistant), while only assistant messages actually carry a phase from the API. Narrowing the type would tighten the contract.
buildAssistantMessageFromResponse takes the last phase encountered when multiple message output items exist in one response — a low-risk edge case given current API behaviour but worth documenting.
Confidence Score: 4/5
This PR is safe to merge; it is a targeted, well-tested bug fix with no security implications.
The change is narrow in scope, has good test coverage across all affected code paths, and introduces no new network calls beyond those already present. The two noted issues (over-broad InputItem type and last-phase-wins aggregation) are style/edge-case concerns rather than bugs that affect current behaviour.
No files require special attention.
Comments Outside Diff (2)
src/agents/openai-ws-connection.ts, line 191-197 (link)
phase permitted on all message roles
The phase field is added to the union member that covers role: "system" | "developer" | "user" | "assistant", but phase is only meaningful for "assistant" role messages. Allowing it on system/developer/user messages could silently accept invalid data from callers.
Consider narrowing the type so only the "assistant" role carries phase:
This makes the type-level contract match the API semantics and lets TypeScript catch accidental misuse.
src/agents/openai-ws-stream.ts, line 304-313 (link)
Last phase wins when multiple message items have different phases
assistantPhase is overwritten on every "message" output item in the loop, so if a response contains — for example — a "commentary" message followed by a "final_answer" message, only "final_answer" is attached to the produced AssistantMessage, even though the aggregated content array holds text from both phases.
In practice the OpenAI Responses API appears to only emit a single phase per output turn, so this is low-risk today. However, it could become a silent correctness bug if that assumption changes. Consider either:
Taking the first non-undefined phase (fail-safe), or
Asserting/logging a warning when two different phases are encountered in the same response.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Describe the problem and fix in 2–5 bullets:
phaseis not preserved across Responses API requestsphasein openclaw representation of Responses API itemsChange Type (select all)
Scope (select all touched areas)
Security Impact (required)