Skip to content

Add Citation DTO core contract to MessagePart - #283

Open
the-hercules wants to merge 3 commits into
WordPress:trunkfrom
the-hercules:fix/277-MessagePart-Citations
Open

Add Citation DTO core contract to MessagePart#283
the-hercules wants to merge 3 commits into
WordPress:trunkfrom
the-hercules:fix/277-MessagePart-Citations

Conversation

@the-hercules

Copy link
Copy Markdown

Implements #277 (Sets up core DTO foundation).
This PR establishes the core DTO foundation to support provider-agnostic source citations, addressing the API-
surface blocker discussed in #277.

Following the feedback and consensus in the issue, this introduces a normalized Citation object and attaches it
to MessagePart. The DTO is shaped to represent the common citation formats from Google, OpenAI, and Anthropic
while keeping the sources checkable:

  • Typed Source Identity: Replaces a generic URI with explicit, nullable url (for remote sources) and
    documentIndex (for request-provided documents) to avoid string sniffing.
  • Text Offset Ranges: Includes startIndex and endIndex to anchor the citation locally to the owning
    MessagePart's text string.
    • Quoted Text: Includes quotedText to cleanly carry Anthropic's block-based cited_text.
    • Validation: Enforces that offset ranges are strictly non-negative and properly ordered (startIndex <= endIndex), throwing an InvalidArgumentException otherwise.

Note: This PR handles the core contract/DTOs within the agnostic client. Follow-up PRs in the respective provider plugin repositories (ai-provider-for-anthropic, etc.) will be required to update their parsing logic to utilize this new format.

AI Disclosure
Model - Gemini 3.1 Pro
Usage - Implementation of functionality and test after discussing in Plan Mode
Human Loop - Reviewed code against the issue's requirements

@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: the-hercules <thehercules@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@codecov

codecov Bot commented Aug 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.42520% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.94%. Comparing base (0a9a1b3) to head (6690dc0).

Files with missing lines Patch % Lines
src/Messages/DTO/MessagePart.php 94.87% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##              trunk     #283      +/-   ##
============================================
+ Coverage     86.60%   86.94%   +0.34%     
- Complexity     1335     1362      +27     
============================================
  Files            68       69       +1     
  Lines          4314     4436     +122     
============================================
+ Hits           3736     3857     +121     
- Misses          578      579       +1     
Flag Coverage Δ
unit 86.94% <98.42%> (+0.34%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@zahidgh zahidgh left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One validation gap: documentIndex represents an index into the request’s documents array, but the constructor and JSON schema currently allow negative values. A Citation(null, -1, ...) can therefore serialize successfully and leave providers to handle an invalid reference. Please reject documentIndex < 0, add minimum: 0 to its schema, and cover the guard with a unit test.

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.

2 participants