Skip to content

Conversation

@mattapperson
Copy link
Collaborator

@mattapperson mattapperson commented Dec 16, 2025

Summary

  • Add tool factory function for creating typed tool definitions
  • Add type inference helpers: InferToolInput, InferToolOutput, InferToolEvent, InferToolEventsUnion
  • Add TypedToolCall and TypedToolCallUnion for typed tool call results
  • Make ResponseWrapper generic with TTools parameter to preserve type information
  • Make callModel generic to flow tool types through the call chain
  • Make event types generic (ToolStreamEvent<TEvent>, ChatStreamEvent<TEvent>, etc.) for typed preliminary results
  • Export all new types from index.ts
  • Add example demonstrating typed tool calling with generator events

How It Works

When using as const with the tools array, all type information flows through:

const weatherTool = tool({
  name: "get_weather",
  inputSchema: z.object({ location: z.string() }),
  outputSchema: z.object({ temperature: z.number() }),
  execute: async (params) => {
    // params is typed as { location: string }
    return { temperature: 72 };
  },
});

const result = client.callModel({
  model: "openai/gpt-4",
  input: "What's the weather?",
  tools: [weatherTool] as const,
});

// Tool calls are typed!
const toolCalls = await result.getToolCalls();
// toolCalls[0].arguments is typed as { location: string }

// Generator events are typed!
for await (const event of result.getToolStream()) {
  if (event.type === "preliminary_result") {
    // event.result is typed based on eventSchema
  }
}

Test plan

  • Build passes (npm run build)
  • Lint passes (npm run lint)
  • Unit tests pass (npx vitest run tests/unit)
  • Type inference works correctly with as const

@mattapperson mattapperson changed the base branch from main to fix-types-and-anthropic-messages December 16, 2025 15:07
- Add tool() function for creating tools with full type inference from Zod schemas
- Auto-detect tool type based on configuration (generator, regular, or manual)
- Add type inference helpers: InferToolInput, InferToolOutput, InferToolEvent
- Add TypedToolCall and TypedToolCallUnion for typed tool call handling
- Add generic type parameters to event types for typed streaming
- Export all tool types and helpers from main index
- Add comprehensive unit tests for create-tool
- Add typed-tool-calling example demonstrating the new API

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
mattapperson and others added 5 commits December 16, 2025 17:26
- Split RegularToolConfig into two types: with and without outputSchema
- Tools without outputSchema now correctly infer return type from execute function
- Fixes 'result' is of type 'unknown' error in tests

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Export all Claude message types from models/index.ts and src/index.ts
- Export CallModelInput type from call-model.ts
- Simplify sdk.ts callModel signature to use CallModelInput type
- Remove unused imports from sdk.ts

Fixes TypeScript compilation errors in CI
Base automatically changed from fix-types-and-anthropic-messages to main December 17, 2025 20:50
Update all E2E tests to explicitly use fromChatMessages() and fromClaudeMessages()
conversion functions instead of passing raw message arrays directly.

This fixes CI validation failures caused by removal of automatic message format
conversion. Now all tests properly demonstrate the required usage pattern:
- Chat-style messages → fromChatMessages()
- Claude-style messages → fromClaudeMessages()

Changes:
- Add imports for fromChatMessages and fromClaudeMessages
- Update ~35 test cases to wrap message arrays in conversion functions
- Fix syntax error (trailing comma) from bulk replacement

Fixes validation errors: "Invalid input: expected string, received array"
Update E2E tests to use fromChatMessages() and fromClaudeMessages() wrappers
for all message array inputs. This fixes CI validation failures by ensuring
all tests explicitly convert message formats before passing to callModel().

Changes:
- Add imports for fromChatMessages and fromClaudeMessages
- Use Python script to automatically wrap input: [...] patterns with fromChatMessages([...])
- Manually wrap Claude message variables with fromClaudeMessages()
- All ~40 test cases now properly use conversion functions

This ensures tests demonstrate the correct SDK usage pattern and pass validation.
@mattapperson mattapperson merged commit 11145ec into main Dec 17, 2025
3 checks passed
@mattapperson mattapperson deleted the matt/fully-typed branch December 17, 2025 22:04
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.

3 participants