Skip to content

Conversation

@mattapperson
Copy link
Collaborator

Summary

  • Replace unsafe as Record<string, unknown> casts with proper type guards and narrowing
  • Remove as unknown as TTools double assertion pattern
  • Consolidate duplicate utility functions and methods
  • Fix non-null assertions with type guards
  • Add comprehensive tests for new type guards

Changes

New Files

  • src/lib/claude-constants.ts - Constants for Claude content block types
  • src/lib/claude-type-guards.ts - Type guards for Claude message detection
  • tests/unit/claude-type-guards.test.ts - 14 tests for type guards
  • tests/unit/tool-types.test.ts - 5 tests for normalizeInputToArray

Refactored

  • callModel.ts: Use imported type guards instead of inline casts
  • response-wrapper.ts: Remove duplicate normalization function, consolidate context methods
  • create-tool.ts: Add isGeneratorConfig type guard
  • tool-orchestrator.ts: Fix non-null assertion with resultHasError guard
  • tool-types.ts: Extend normalizeInputToArray to handle undefined

Test plan

  • All unit tests pass (34 tests)
  • TypeScript compiles without errors
  • No as unknown as T double assertions remain
  • No non-null assertions on error fields remain

🤖 Generated with Claude Code

@mattapperson mattapperson changed the title refactor: replace type casts with proper type guards refactor: replace type casts with proper type guards, and add nextTurn config to tools Dec 16, 2025
mattapperson and others added 3 commits December 17, 2025 16:46
- Add createTool, createGeneratorTool, createManualTool factory functions
- Add type inference helpers: InferToolInput, InferToolOutput, InferToolEvent
- Add TypedToolCall and TypedToolCallUnion for typed tool call results
- Add InferToolEventsUnion for typed generator event streams
- Make ResponseWrapper generic with TTools parameter
- Make callModel generic to preserve tool types through call chain
- Make event types generic (ToolStreamEvent, ChatStreamEvent, etc.)
- Export all new types from index.ts
- Add example demonstrating typed tool calling

When using 'as const' with tools array, all type information flows through:
- toolCall.arguments is typed based on inputSchema
- event.result in streams is typed based on eventSchema

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Improve isClaudeStyleMessages() heuristic to check ALL messages for
  Claude-specific features (tool_result, image with source, tool_use)
- Properly convert tool_use blocks to ResponsesOutputItemFunctionCall
- Convert image blocks in user messages to OpenResponsesInputMessageItem
- Handle assistant images via synthetic function call outputs
- Add TODO comment for cache_read_input_tokens mapping
- Add console.warn for JSON parsing failures in tool arguments
- Add E2E tests for getClaudeMessage() output format

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Create claude-constants.ts with constants for Claude block types
- Create claude-type-guards.ts with isClaudeStyleMessages type guard
- Replace `as Record<string, unknown>` casts with proper type narrowing
- Remove `as unknown as TTools` double assertion in callModel.ts
- Consolidate duplicate normalizeInputToArray functions
- Consolidate createTurnContext and createTurnContextWithMutations methods
- Add isGeneratorConfig type guard in create-tool.ts
- Fix non-null assertion in tool-orchestrator.ts with resultHasError guard
- Extend normalizeInputToArray to handle undefined input
- Add comprehensive tests for new type guards

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@mattapperson mattapperson force-pushed the matt/param-altering-tools branch from 0a3d75d to f2c3d0d Compare December 17, 2025 21:54
Base automatically changed from matt/fully-typed to main December 17, 2025 22:03
Resolved conflicts by preferring main's changes:
- examples/callModel-typed-tool-calling.example.ts
- src/index.ts
- src/lib/stream-transformers.ts
- src/lib/tool-types.ts
- src/sdk/sdk.ts
- tests/unit/create-tool.test.ts

Will fix any type errors and adapt to new patterns from main.
- Removed create-tool.ts (replaced by tool.ts in main)
- Restored tool-orchestrator.ts from main
- Restored model-result.ts from main (uses ModelResult class name)

Build now passes with no type errors.
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