Skip to content

feat(openai-protocol): add ResponseTool::as_str()#1457

Merged
slin1237 merged 1 commit into
mainfrom
feat/openai-protocol-response-tool-kind-str
May 6, 2026
Merged

feat(openai-protocol): add ResponseTool::as_str()#1457
slin1237 merged 1 commit into
mainfrom
feat/openai-protocol-response-tool-kind-str

Conversation

@slin1237
Copy link
Copy Markdown
Collaborator

@slin1237 slin1237 commented May 6, 2026

Summary

Replace the 14-arm ResponseTool::* => &str match in harmony/builder.rs with a one-liner that calls tool.as_str(). The new method lives in openai-protocol next to the enum, matches the as_str convention used by ItemType etc., and returns each variant's #[serde(rename = ...)] tag verbatim.

Motivation

Adding a new ResponseTool variant (e.g. a future hosted family) used to require remembering to update the harmony match. Forgetting it would mean the new variant's wire tag silently became whatever the catch-all of the day was — and there was no compile error to flag the omission. Moving the table to the enum's owning crate makes the match exhaustive in openai-protocol, so adding a variant there fails compilation until as_str is updated.

This is the follow-up the PR #1455 audit flagged as out-of-scope — the harmony match wasn't a bridge concern, it was generic enum hygiene.

Test plan

  • New unit test response_tool_as_str_matches_serde_tag_for_unit_variants pins the three unit variants (Computer, ApplyPatch, LocalShell) against a serde roundtrip so as_str can't drift from the canonical wire tag
  • cargo test -p openai-protocol --lib — passes
  • cargo test -p smg --lib — 751 passed
  • cargo clippy -p smg -p openai-protocol --lib --tests -- -D warnings — clean
  • cargo fmt — clean

Summary by CodeRabbit

  • New Features

    • Added helper methods to response APIs for improved type handling and content construction.
  • Tests

    • Added unit tests to validate response tool type conversion functionality.
  • Refactor

    • Simplified internal tool type mapping logic.

`harmony/builder.rs` carried a 14-arm match converting `ResponseTool`
variants to their wire `type` tags. The strings are exactly the
variants' `#[serde(rename = ...)]` attributes — generic enum-to-tag
data masquerading as harmony-specific routing logic. Adding a new
`ResponseTool` variant required remembering to update this match;
nothing flagged it.

Add `ResponseTool::as_str(&self) -> &'static str` next to the enum,
matching the `as_str` convention used by `ItemType` and friends.
Migrate the harmony match to a one-liner. The match in `as_str` is
exhaustive — adding a variant fails compilation in `openai-protocol`
itself, where the vocabulary lives.

Test pins the unit variants (Computer, ApplyPatch, LocalShell)
against a serde roundtrip so `as_str` can't drift from the
canonical wire tag.

Signed-off-by: Simo Lin <25425177+slin1237@users.noreply.github.com>
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@github-actions github-actions Bot added grpc gRPC client and router changes protocols Protocols crate changes model-gateway Model gateway crate changes labels May 6, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 6, 2026

📝 Walkthrough

Walkthrough

This PR centralizes tool type-to-string mapping by adding a ResponseTool::as_str() method that returns the OpenAI wire type for each variant, introduces a helper ResponseReasoningContent::new_reasoning_text() constructor, and refactors the Harmony builder to use the new method instead of explicit variant matching.

Changes

Tool Type String Mapping & Helper Methods

Layer / File(s) Summary
Type Methods
crates/protocols/src/responses.rs
Adds ResponseTool::as_str() returning the serde tag string for all 14 variants (Function, WebSearchPreview, WebSearch, CodeInterpreter, Mcp, FileSearch, ImageGeneration, Computer, ComputerUsePreview, Custom, Namespace, Shell, ApplyPatch, LocalShell); adds ResponseReasoningContent::new_reasoning_text() constructor.
Builder Integration
model_gateway/src/routers/grpc/harmony/builder.rs
Harmony builder's tool type mapping simplified from explicit exhaustive match to tools.iter().map(ResponseTool::as_str).collect().
Tests & Validation
crates/protocols/src/responses.rs
Unit tests verify ResponseTool::as_str() matches serde "type" tags for unit variants (Computer, ApplyPatch, LocalShell).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • lightseekorg/smg#1337: Adds ResponseTool::Namespace variant that is now mapped via the new as_str() method.
  • lightseekorg/smg#1301: Adds ResponseTool::Custom variant which is included in the new as_str() exhaustive mapping.
  • lightseekorg/smg#1304: Modifies ResponseTool handling and Harmony builder tool mapping that now leverages the new as_str() method.

Suggested labels

protocols, model-gateway, openai, grpc, tests

Suggested reviewers

  • key4ng
  • CatherineSue
  • claude

Poem

🐰 A string in each tool's heart now beats,
No matching arms, just clean retreats,
Harmony flows with as_str's might,
Where serde tags meet builder's light!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding a ResponseTool::as_str() method to the openai-protocol crate, which is the primary objective of the pull request.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/openai-protocol-response-tool-kind-str

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

Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

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

Clean refactoring. All as_str() return values verified against their #[serde(rename)] attributes — no drift. Builder replacement is equivalent. LGTM.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/protocols/src/responses.rs`:
- Around line 3915-3929: The test
response_tool_as_str_matches_serde_tag_for_unit_variants only covers unit
variants; extend it to also instantiate a few payload-carrying variants of
ResponseTool (e.g., the Browser and any Tool/External variants that carry
fields) and include them in the loop where you call serde_json::to_value(&tool)
and compare tool.as_str() with the serialized "type" tag; ensure you create
minimal valid instances for those payload variants so the serialization succeeds
and the assertion checks parity for both unit and payload variants using the
same serde_json::to_value / get("type") / as_str() pattern.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 5dcecd51-ca5b-4bbc-b875-334b13468038

📥 Commits

Reviewing files that changed from the base of the PR and between d83593d and a45cb40.

📒 Files selected for processing (2)
  • crates/protocols/src/responses.rs
  • model_gateway/src/routers/grpc/harmony/builder.rs

Comment thread crates/protocols/src/responses.rs
@slin1237 slin1237 merged commit 45e4138 into main May 6, 2026
49 checks passed
@slin1237 slin1237 deleted the feat/openai-protocol-response-tool-kind-str branch May 6, 2026 04:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

grpc gRPC client and router changes model-gateway Model gateway crate changes protocols Protocols crate changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant