Skip to content

refactor: add type-safe command interface to appInvoke#13

Merged
yejiming merged 2 commits into
yejiming:mainfrom
whitelonng:fix/05-appinvoke-type-safety
Jun 22, 2026
Merged

refactor: add type-safe command interface to appInvoke#13
yejiming merged 2 commits into
yejiming:mainfrom
whitelonng:fix/05-appinvoke-type-safety

Conversation

@whitelonng

Copy link
Copy Markdown
Contributor

Summary

Fixes type safety issue where appInvoke used unsafe string cmd and any args, allowing typos, wrong argument types, and incorrect return types to pass type checking.

Changes

Type System

  • Add AppInvokeCommands interface defining all commands with typed args and results
  • Update appInvoke signature to use conditional types for type-safe invocation
  • Command names are now autocompleted and typo-checked at compile time
  • Argument structure is validated for each command
  • Return types are inferred automatically, no manual type annotations needed

Code Updates

  • Remove all generic type parameters from appInvoke call sites (33 locations across 7 files)
  • Add savedAt field to list_agent_sessions result type to match actual schema
  • Update internal implementation to use typed cmdArgs instead of loose args

Example

Before (unsafe):

const result = await appInvoke<any>('list_sessions', { prefix: 'typo' });
// No compile error, but crashes at runtime with "Unknown command"

After (type-safe):

const result = await appInvoke('list_agent_sessions', { prefix: 'test' });
// Type error if command name or args are wrong
// Return type is { id: string; title: string; ... }[] automatically

Benefits

  • ✅ Eliminates entire class of runtime errors from incorrect API usage
  • ✅ Better IDE autocomplete and refactoring support
  • ✅ Catches typos and API misuse at compile time
  • ✅ Self-documenting: command structure is defined in one place

Test Plan

  • ✅ 186 Rust unit tests pass
  • ✅ TypeScript type check passes (all previous type errors resolved)
  • Manual testing: All appInvoke calls now have proper type inference

Breaking Changes

None for runtime behavior. Type-level only: code that was previously using incorrect command names or arguments will now show TypeScript errors (which is the intended improvement).

Type safety issue: appInvoke used unsafe string cmd and any args, allowing
typos, wrong argument types, and incorrect return types to pass type checking.

Changes:
- Add AppInvokeCommands interface defining all commands with typed args and results
- Update appInvoke signature to use conditional types for type-safe invocation
- Remove all generic type parameters from appInvoke call sites (33 locations)
- Add 'savedAt' field to list_agent_sessions result type to match actual schema
- Update internal implementation to use typed cmdArgs instead of loose 'args'

Benefits:
- Command names are now autocompleted and typo-checked at compile time
- Argument structure is validated for each command
- Return types are inferred automatically, no manual type annotations needed
- Eliminates entire class of runtime errors from incorrect API usage

Example before:
  const result = await appInvoke<any>('list_sessions', { prefix: 'typo' });
  // No error, but crashes at runtime

Example after:
  const result = await appInvoke('list_agent_sessions', { prefix: 'test' });
  // Type error if command name or args are wrong, result type is inferred

Test results:
- ✅ 186 Rust unit tests pass
- ✅ TypeScript type check passes
@whitelonng whitelonng force-pushed the fix/05-appinvoke-type-safety branch from ee47619 to 0b9bfa4 Compare June 21, 2026 14:29
@yejiming yejiming merged commit 9ca2728 into yejiming:main Jun 22, 2026
2 checks passed
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