Skip to content

Core hardening (OCP/DIP): neutral Usage value object and slim tool contract - #23

Merged
MarcelloDuarte merged 1 commit into
mainfrom
v0.12-core-hardening
Jul 25, 2026
Merged

Core hardening (OCP/DIP): neutral Usage value object and slim tool contract#23
MarcelloDuarte merged 1 commit into
mainfrom
v0.12-core-hardening

Conversation

@MarcelloDuarte

Copy link
Copy Markdown
Contributor

What

Backward-compatible OCP/DIP hardening of papi-core. No public method is removed; the change is safe for existing consumers.

Neutral Usage value object

  • New Usage (inputTokens, outputTokens, totalTokens, raw), implementing ArrayAccess over the legacy keys so $response->usage['input_tokens'] keeps working.
  • Response and EmbeddingResponse store a Usage and accept Usage|array (array path deprecated).
  • Bug fix: the token getters (getInputTokens() etc.) now read the normalised Usage, so they return correct counts for OpenAI-family providers and Cohere (previously 0).

Neutral tool flow

  • Removed toAnthropic()/toOpenAI() from ToolInterface; kept them @deprecated on the concrete Tool.
  • Agent now emits a neutral tool definition (name, description, parameters); each provider formats it to its own wire shape.

Provider-owned response mapping

  • Response::fromAnthropic() and ToolCall::fromAnthropic() are @deprecated; Anthropic mapping now lives in the anthropic package. Response::fromOpenAI() stays as the shared OpenAI-compatible adapter.

Compatibility

Fully backward compatible: deprecations are docblock-only (no runtime notices), and every previously public method still exists.

Tests

composer ci green: 207 tests passed, lint and Psalm (level 4) clean.

Add a neutral Usage value object; Response and EmbeddingResponse store it and
accept Usage|array, with ArrayAccess preserving legacy key access. This fixes
token counts returning 0 for OpenAI-family providers and Cohere.

Remove toAnthropic() and toOpenAI() from ToolInterface; keep them deprecated on
the concrete Tool. Agent now emits a neutral tool definition (name, description,
parameters) and each provider formats it.

Deprecate Response::fromAnthropic() and ToolCall::fromAnthropic(); provider
response mapping now lives in the provider packages.

No public method removed; fully backward compatible.
@MarcelloDuarte
MarcelloDuarte merged commit 166601c into main Jul 25, 2026
6 checks passed
@MarcelloDuarte
MarcelloDuarte deleted the v0.12-core-hardening branch July 25, 2026 19:55
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