Skip to content

fix: extract leading system messages for AI SDK calls#42

Merged
Moskize91 merged 4 commits into
mainfrom
fix/extract-system-messages-refine
May 6, 2026
Merged

fix: extract leading system messages for AI SDK calls#42
Moskize91 merged 4 commits into
mainfrom
fix/extract-system-messages-refine

Conversation

@Moskize91

Copy link
Copy Markdown
Contributor

Summary

  • extract leading system messages only at AI SDK call construction time
  • pass extracted system prompts through the top-level system field for generateText and streamText
  • add coverage for top-level system extraction, non-leading system preservation, and cache-key stability

Why

AI SDK warns when system messages are passed inside messages. This change keeps the original request messages intact for cache-key calculation, and only rewrites the outbound generationInput shape before calling the SDK.

Verification

  • pnpm --dir ./project test:run -- test/llm/client.test.ts
  • pnpm --dir ./project typecheck

@coderabbitai

coderabbitai Bot commented May 6, 2026

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a7dd2a25-58a9-4322-90d5-7b4d990dc280

📥 Commits

Reviewing files that changed from the base of the PR and between 8ee36f4 and aecce2f.

📒 Files selected for processing (1)
  • test/llm/client.test.ts

Summary by CodeRabbit

  • Improvements

    • Enhanced system-message handling so leading system prompts are surfaced correctly and conversational context is more consistent.
    • Improved caching to reuse responses for identical inputs, reducing redundant requests and improving performance.
  • Tests

    • Added tests validating system-message normalization, caching behavior, and preservation of non-leading system messages.

Walkthrough

The PR adds normalization of initial system messages for LLM generation requests via a new normalizeGenerationInput function. Leading system messages are extracted from the messages array and placed into a top-level system field (string or SystemModelMessage/array), while non-leading system messages remain unchanged. generationInput construction now uses this normalization. Tests were added to verify leading-system extraction, cache key behavior based on original messages, and preservation of non-leading system messages.

Sequence Diagram(s)

sequenceDiagram
  participant Client as Client
  participant Normalizer as normalizeGenerationInput
  participant Cache as Cache
  participant LLM as LLM.generateText

  Client->>Normalizer: provide generation input (messages, model, etc.)
  Normalizer-->>Client: normalized input (messages, optional system)
  Client->>Cache: lookup cache using original messages key
  alt cache hit
    Cache-->>Client: cached response
  else cache miss
    Client->>LLM: call generateText(normalized input)
    LLM-->>Client: generated response
    Client->>Cache: store response with original messages key
  end
  Client-->>Client: return response to caller
Loading
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title follows the required format with type and subject, clearly describing the main change: extracting leading system messages for AI SDK calls.
Description check ✅ Passed The description is directly related to the changeset, explaining the purpose, implementation approach, and verification steps for the system message extraction feature.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch fix/extract-system-messages-refine

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Comment @coderabbitai help to get the list of available commands and usage tips.

@Moskize91 Moskize91 merged commit 0ad3929 into main May 6, 2026
2 checks passed
@Moskize91 Moskize91 deleted the fix/extract-system-messages-refine branch May 6, 2026 03:01
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