Skip to content

πŸ§‘β€πŸ« refactor: Share Stream Frame Placement Rules - #18194

Merged
danny-avila merged 1 commit into
devfrom
danny-avila/stream-part-placement
Nov 17, 2026
Merged

danny-avila merged 1 commit into
devfrom
danny-avila/stream-part-placement

Conversation

@danny-avila

Copy link
Copy Markdown
Collaborator

Summary

A stream carries frames that are not token deltas but still write to the response: an injected steer, an activity label, a reasoning title, a pause for approval or a question, a pending MCP OAuth prompt. Main chat places them through ResponsePatcher, and the decision of where each one lands (which message, which content index, how an edit's retained prefix shifts it, whether more of the target has yet to be drawn) was written inline in the patcher, mixed with things only the hooks session needs: frame retries, flushing queued token writes, re-syncing the step handler's copy. The part transforms themselves reached the patcher through a host member, ResumableParts, even though all but one already lived in this package and the app only handed them back.

That shape means a second consumer cannot place these frames without copying the patcher. The Chat class is that second consumer: its event applier (#18190) leaves these frames unapplied for exactly this reason.

This separates the two. core/parts/place.ts holds one pure function per frame, each answering where the frame lands and whether it is settled, and ResponsePatcher keeps only the session's own concerns around them. Behavior is unchanged.

Sixth in a stack, on #18193. The Chat applier calls the same functions in the next PR.

How it works

Every placement has one signature and one result:

placeSteer(messages, event, context)            // injected steer part
placeActivityLabel(messages, event, context)    // label at its claimed index, phase bounds shifted with it
placeReasoningLabel(messages, event, context)   // title onto its reasoning part
placePendingAction(messages, action)            // approval or question controls
oauthPromptSteps(prompt)                        // the two step events that draw a paused OAuth tool call

type PlacementContext = { placeholderIds?, prefixLength?, foldedFirstPart? }
type Placement = { index, message, changed, settled }   // undefined: the response is not in the list

settled: false replaces three differently worded retry conditions: a tool approval whose sibling calls are not all drawn, a question whose part was not written, a reasoning title whose reasoning part does not exist yet. What a caller does about it is its own business: the patcher asks for another frame, as before.

What stays in the patcher is what belongs to the hooks session:

ResponsePatcher.applySteer
  sub.isCurrent()                 fence
  host.steers.resolve(...)        the composer's chip
  host.events.flushPendingDeltas  queued token writes land first
  placeSteer(...)                 <- shared
  commit + syncSubmissionPlaceholder, or retry on a later frame

ResumableParts is removed from ResumableHost: the patcher imports the transforms. The one transform that still lived in the app, applyReasoningLabel with findReasoningLabelMessageIndex, moves to core/parts/reasoningLabels.ts with its test; ~/utils re-exports both, so app imports are unchanged.

Type of change

  • Refactor
  • Tests / tooling / CI

Testing

Tested environments/configuration:

Unit level. No behavior change is intended, so the evidence is main chat's existing suites passing on the new code.

Automated tests:

  • Added packages/chat/src/core/__tests__/place.spec.ts (15 cases): each placement with no prefix, with a retained prefix, under a placeholder id, with the response absent, replayed, and unsettled; the phase start that moves back only when the generation's first part merged into the prefix; the OAuth prompt's two step events.
  • patches.spec.ts: the three cases that stubbed the host's part functions now assert the message the patcher wrote, with the real transforms.
  • reasoningLabels.spec.ts moved from the client with its subject.
  • packages/chat: npx jest src/core src/react/stream (73 suites, 1700 tests); npx tsc --noEmit.
  • client, against the rebuilt package: useResumableSSE.spec.ts, useResumeOnLoad.spec.tsx and Chat/__tests__/transport.spec.tsx (245 tests), which drive real steers, labels and pauses through the patcher.
  • npm run static-checks -- --against origin/dev.

Screenshots / recordings

No user-facing change.

Risk / compatibility

This is main chat's placement path. The logic moved rather than changed: the same target lookup, the same prefix arithmetic, the same no-op identity (changed is message !== messages[index]), the same retry conditions expressed as settled. ResumableHost loses the parts member, which is a breaking change for any other host of the package; the app is the only one.

@danny-avila
danny-avila added this pull request to stack #18259 November 17, 2026 14:36
@danny-avila
danny-avila force-pushed the danny-avila/stream-part-placement branch 2 times, most recently from 24cc4a7 to 57700ea Compare November 17, 2026 15:05
@lia-by-librechat lia-by-librechat Bot changed the title πŸ“ refactor: Place Stream Side Frames With One Set of Functions πŸ§‘β€πŸ« refactor: Share Stream Frame Placement Rules Nov 17, 2026
@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: βœ… Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
πŸ“ Code Review βœ… Completed 2026-11-17T16:49:35.545139Z d3961c1 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with πŸ‘€ while any review is running, comments if it has suggestions, and reacts with πŸ‘ once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 57700ea02a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with πŸ‘.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danny-avila
danny-avila force-pushed the danny-avila/stream-part-placement branch 3 times, most recently from 850e9b1 to eabef10 Compare November 17, 2026 15:38
@danny-avila
danny-avila force-pushed the danny-avila/stream-part-placement branch 2 times, most recently from dad1dfb to c99adae Compare November 17, 2026 15:48
@danny-avila
danny-avila force-pushed the danny-avila/stream-part-placement branch 2 times, most recently from 1c64a9b to 9763a87 Compare November 17, 2026 16:10
@danny-avila
danny-avila marked this pull request as ready for review November 17, 2026 16:11
@danny-avila
danny-avila force-pushed the danny-avila/stream-part-placement branch from 9763a87 to 518b90d Compare November 17, 2026 16:15
@danny-avila
danny-avila force-pushed the danny-avila/stream-part-placement branch from 518b90d to 4a81f11 Compare November 17, 2026 16:30
Base automatically changed from danny-avila/followed-thread-view to dev November 17, 2026 16:48
@danny-avila
danny-avila force-pushed the danny-avila/stream-part-placement branch from 4a81f11 to d3961c1 Compare November 17, 2026 16:48
@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review

Head d3961c1 (rebased onto dev after #18193 merged).

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: d3961c1559

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with πŸ‘.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danny-avila
danny-avila merged commit c1c108a into dev Nov 17, 2026
64 of 65 checks passed
@danny-avila
danny-avila deleted the danny-avila/stream-part-placement branch November 17, 2026 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant