Skip to content

refactor: use discriminated union for all multi-operation agent tools#814

Merged
larryro merged 2 commits into
mainfrom
refactor/discriminated-union-agent-tools
Mar 18, 2026
Merged

refactor: use discriminated union for all multi-operation agent tools#814
larryro merged 2 commits into
mainfrom
refactor/discriminated-union-agent-tools

Conversation

@larryro

@larryro larryro commented Mar 18, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Replace flat z.object() schemas with z.discriminatedUnion('operation', [...]) across all 9 multi-operation agent tools, matching the pattern already established in image_tool.ts
  • Required fields per operation are now enforced at schema level (Zod validation), removing redundant runtime checks
  • Add .nonempty() validation to productIds in product_read_tool to preserve fail-fast behavior for empty arrays

Tools refactored

Tool Operations Key improvements
docx list_templates, generate, parse fileName required in generate; fileId/user_input required in parse
excel generate, parse fileName/sheets required in generate; fileId required in parse
pdf generate, parse fileName/sourceType/content required in generate; fileId/user_input required in parse
text parse, generate filename/content required in generate; fileId/user_input required in parse
pptx list_templates, generate, parse fileName/slidesContent required in generate; fileId/user_input required in parse
product_read get_by_id, list, count productIds (nonempty) required in get_by_id
customer_read get_by_id, get_by_email, list, count customerId required in get_by_id; email required in get_by_email
workflow_read get_structure, get_step, list_all, get_active_version_steps, list_version_history Required IDs/names enforced per variant
database_schema list_tables, get_table_schema tableName required in get_table_schema

Test plan

  • npx tsc --noEmit — zero type errors
  • bun run --filter @tale/platform lint — zero warnings/errors
  • Verify agent tool calls work end-to-end in staging

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Restructured argument validation across customer, product, workflow, and file management tools for improved type safety and clarity.
    • Enhanced operation-specific field requirements to provide more precise validation and error handling for each tool variant.

larryro added 2 commits March 19, 2026 07:13
…args

Replace flat z.object schemas with z.discriminatedUnion('operation', [...])
across all 9 multi-operation agent tools: docx, excel, pdf, text, pptx,
product_read, customer_read, workflow_read, and database_schema.

This provides better type safety via TypeScript narrowing, enforces
required fields per operation at the schema level, and removes redundant
runtime validation checks.
- product_read_tool: add .nonempty() to productIds schema to reject
  empty arrays (previously caught by runtime check)
- excel_tool: remove unnecessary `as const` cast and dead
  `?? 'unknown.xlsx'` fallback (fileName is now required)

@greptile-apps greptile-apps Bot 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.

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@larryro larryro merged commit 41f1768 into main Mar 18, 2026
16 of 17 checks passed
@larryro larryro deleted the refactor/discriminated-union-agent-tools branch March 18, 2026 23:25
@coderabbitai

coderabbitai Bot commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

This PR refactors argument schemas across nine agent tool files by replacing flat object schemas with discriminated unions based on the operation field. Each operation variant now defines its own required and optional fields rather than using a single schema with optional fields. Runtime validation checks for required fields (e.g., presence of customerId, fileName, workflowId) are removed, delegating validation to the schema structure. Handler logic is simplified to rely on the discriminated union for type narrowing and field availability.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~70 minutes

The PR applies a consistent schema refactoring pattern across nine heterogeneous files (customer, database, docx, excel, pdf, pptx, text, product, and workflow tools), each with distinct operation variants and field requirements. While the refactoring follows a unified pattern, reviewers must verify that each discriminated union correctly captures per-operation field requirements, that all removed runtime checks are appropriately replaced by schema validation, and that handler branching logic correctly aligns with the new schema structure. The spread across multiple tools and the mixture of schema, validation, and control flow changes increases cognitive load despite the consistency of the pattern.

Possibly related PRs

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the main refactoring: converting flat object schemas to discriminated unions across all multi-operation agent tools, which is the primary and most significant change across the nine modified files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ 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 refactor/discriminated-union-agent-tools
📝 Coding Plan
  • Generate coding plan for human review comments

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

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