Skip to content

Commit 90619cc

Browse files
committed
refactor(llm): tighten reasoningStart step parity and lock itemId-only drop
Two narrow changes prompted by the latest review pass — both verified against the surrounding code, not added arbitrarily: - reasoningStart now calls stepStart unconditionally before checking whether the reasoning id is already in state. textDelta, textEnd, and reasoningEnd already do this; reasoningStart was the outlier. stepStart is idempotent so the change is a no-op for all existing callers, but it prevents a hypothetical future protocol whose first emitted event is a reasoningDelta from being missing a step-start. This is internal helper hygiene; it does not change anything on the wire and adds no OpenAI-imposed constraint. - Add a regression test asserting that a reasoning part with an itemId but no encrypted state is dropped under store:false. This codifies behaviour the protocol already enforces and that the surrounding comment in lowerMessages documents: "With store:false, OpenAI only accepts previous reasoning items when the encrypted state is present." Complements the existing reasoningEncryptedContent: null case.
1 parent ae89813 commit 90619cc

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎packages/llm/src/protocols/utils/lifecycle.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ export const reasoningStart = (
3030
id: string,
3131
providerMetadata?: ProviderMetadata,
3232
): State => {
33-
if (state.reasoning.has(id)) return state
33+
// Call stepStart unconditionally so the very first event of a response
34+
// can be a reasoningDelta and still produce a step-start, matching
35+
// textDelta/textEnd/reasoningEnd parity.
3436
const stepped = stepStart(state, events)
37+
if (stepped.reasoning.has(id)) return stepped
3538
events.push(LLMEvent.reasoningStart({ id, providerMetadata }))
3639
return { ...stepped, reasoning: new Set([...stepped.reasoning, id]) }
3740
}

‎packages/llm/test/provider/openai-responses.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,40 @@ describe("OpenAI Responses route", () => {
933933
}),
934934
)
935935

936+
// Complementary to the previous case: a reasoning part with an `itemId`
937+
// but no `reasoningEncryptedContent` key at all. Under store:false the
938+
// request cannot replay reasoning state, so the item is dropped.
939+
it.effect("drops reasoning parts with an itemId but no encrypted state under store:false", () =>
940+
Effect.gen(function* () {
941+
const prepared = yield* LLMClient.prepare(
942+
LLM.request({
943+
id: "req_reasoning_itemid_only",
944+
model,
945+
messages: [
946+
Message.user("What changed?"),
947+
Message.assistant([
948+
{
949+
type: "reasoning",
950+
text: "Checked the previous diff.",
951+
providerMetadata: { openai: { itemId: "rs_1" } },
952+
},
953+
{ type: "text", text: "The parser changed." },
954+
]),
955+
],
956+
providerOptions: { openai: { store: false } },
957+
}),
958+
)
959+
960+
expect(prepared.body).toMatchObject({
961+
input: [
962+
{ role: "user", content: [{ type: "input_text", text: "What changed?" }] },
963+
{ role: "assistant", content: [{ type: "output_text", text: "The parser changed." }] },
964+
],
965+
store: false,
966+
})
967+
}),
968+
)
969+
936970
it.effect("assembles streamed function call input", () =>
937971
Effect.gen(function* () {
938972
const body = sseEvents(

0 commit comments

Comments
 (0)