fix: preserve plugin metadata and title in fromPlugin wrapper#18631
fix: preserve plugin metadata and title in fromPlugin wrapper#18631Haohao-end wants to merge 1 commit into
Conversation
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
|
All checks are passing now. Happy to make follow-up changes if any feedback comes up. |
atharvau
left a comment
There was a problem hiding this comment.
Code Review
✅ APPROVED - Important bug fix with comprehensive solution
This PR fixes a significant issue where plugin metadata and titles were being lost during tool execution. The implementation is well-thought-out and includes proper test coverage.
What this PR does well:
- Comprehensive fix: Addresses metadata loss from both context.metadata() calls and structured plugin returns
- Backward compatibility: Maintains support for legacy string-only plugin returns
- Robust error handling: Validates return types and provides clear error messages
- Excellent test coverage: Three comprehensive test cases cover all scenarios
- Type safety: Uses proper type guards for runtime validation
Code Quality Assessment:
No Critical Issues Found ✅
No Security Issues Found ✅
No Performance Issues Found ✅
Technical Analysis:
Strengths:
- State tracking: Captures metadata state via closure variables (title, meta)
- Flexible input handling: Supports both structured objects and plain strings
- Proper merging: Truncation metadata correctly overlays plugin metadata
- Type validation: Runtime checks ensure safe property access
Architecture:
- Good: Clean separation between plugin context enhancement and result processing
- Good: Maintains existing truncation behavior while preserving metadata
- Good: Error handling guides developers toward correct return format
Code Deep Dive:
// Smart return type detection
const item = plain(result) ? result : undefined
const text = item && typeof item.output === "string" ? item.output :
typeof result === "string" ? result : undefinedThis logic properly handles both legacy (string) and new (structured) return formats.
Minor Suggestions:
- Consider extracting the type guard
plain()to a shared utility if used elsewhere - The error message could include the actual return type for better debugging
Test Coverage Analysis:
Excellent test coverage with three scenarios:
- ✅ Metadata set via context.metadata()
- ✅ String plugin results (legacy)
- ✅ Structured plugin results (new format)
All tests properly verify title, output, metadata, and truncation behavior.
Overall Assessment: This is a high-quality fix that resolves a real issue affecting plugin developers. The implementation is robust and well-tested.
Issue for this PR
Closes #12527
Type of change
What does this PR do?
This fixes plugin tool result handling in
fromPlugin()so metadata and title set during execution are preserved in the final tool result.Previously, the wrapper rebuilt the final result with only truncation metadata, which caused metadata set via
context.metadata()to be lost. It also treated plugin results as plain strings and dropped structured fields liketitleandmetadata.This change:
context.metadata()title/metadata as fallback stateoutput,title, andmetadataHow did you verify your code works?
I verified this locally with:
cd ~/opencode-12527/packages/opencodebun test test/tool/registry.test.tsbun run typecheckI also added regression coverage for:
context.metadata()Screenshots / recordings
N/A
Checklist