Skip to content

fix: enhance tokenizer and add comprehensive tool support #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

nghyane
Copy link

@nghyane nghyane commented Jun 10, 2025

Overview

This PR addresses several critical issues and adds comprehensive tool support to the copilot-api wrapper.

Issues Fixed

Changes Made

1. Enhanced Tokenizer (src/lib/tokenizer.ts)

  • Fix null content handling: Prevents crashes when message.content is null (common in tool call messages)
  • Support for ContentPart arrays: Properly extracts text content from complex message structures
  • Tool call token counting: Converts tool calls to text representation for accurate token counting
  • Multi-role support: Handles tool, assistant, user, and system message roles correctly
  • Robust error handling: Gracefully handles edge cases and malformed messages

2. Comprehensive Tool Support (src/services/copilot/create-chat-completions.ts)

  • Complete tool/function calling API: Added support for tools, tool_choice, and tool responses
  • Enhanced TypeScript types: Added Tool, ToolCall, ToolChoice, and DeltaToolCall interfaces
  • Better error handling: Specific error messages when tools are not supported by GitHub Copilot
  • Message processing improvements: Better handling of tool-related message types
  • Finish reason support: Added support for tool_calls, length, and content_filter finish reasons

3. Safety Improvements (src/routes/chat-completions/handler.ts)

  • Null safety: Added null checks before calling getTokenCount
  • Defensive programming: Prevents crashes when payload.messages is undefined

4. Code Quality (src/lib/api-config.ts)

  • Simplified syntax: Improved default parameter handling in copilotHeaders

Testing

The changes have been tested with:

  • Messages with null content (tool call scenarios)
  • Complex message structures with ContentPart arrays
  • Tool calling workflows
  • Standard chat completions (backward compatibility maintained)

Backward Compatibility

Fully backward compatible - All existing functionality continues to work as before. These changes only enhance existing capabilities and fix edge cases.

Technical Details

Tokenizer Enhancements

The tokenizer now properly handles:

// Before: Would crash on null content
// After: Safely converts to text representation
const toolCallMessage = {
  role: "assistant",
  content: null,
  tool_calls: [{ function: { name: "get_weather", arguments: "{...}" } }]
}

Tool Support

Added complete OpenAI-compatible tool calling:

// Now supported
const payload = {
  messages: [...],
  tools: [{
    type: "function",
    function: {
      name: "get_weather",
      description: "Get weather information",
      parameters: { ... }
    }
  }],
  tool_choice: "auto"
}

Impact

  • Stability: Fixes crashes related to tokenizer with complex messages
  • Functionality: Enables tool/function calling capabilities
  • Compatibility: Maintains full backward compatibility
  • User Experience: Better error messages and more robust operation

This PR significantly enhances the robustness and functionality of copilot-api while maintaining full backward compatibility.


Pull Request opened by Augment Code with guidance from the PR author

“nghyane” added 4 commits June 10, 2025 19:51
- Fix null content handling in getTokenCount (fixes ericc-ch#21)
- Add support for tool calls and content parts in token counting
- Improve token counting accuracy for different message roles
- Add null safety checks for message processing
- Handle ContentPart arrays and extract text content properly
- Add tool/function calling support to chat completions (addresses ericc-ch#30)
- Enhanced error handling for unsupported tool features
- Support for tool choice and tool responses in message types
- Better message processing for tool-related content
- Add proper TypeScript types for Tool, ToolCall, and ToolChoice
- Improve vision capability detection logic
- Simplify default parameter syntax in copilotHeaders
- Fix undefined error when accessing selectedModel.capabilities.limits.max_output_tokens
- Add proper optional chaining to prevent crashes when model is not found
- Ensures graceful handling when state.models or selectedModel is undefined
“nghyane” added 11 commits June 15, 2025 15:56
- Enhanced tokenizer with better model support and fallback mechanisms
- Added comprehensive logging system with global logger
- Improved model utilities and streaming capabilities
- Added format converter for better data handling
- Enhanced error handling and debugging capabilities
- Updated dependencies and configurations for better compatibility
- Remove format-converter.ts and replace with format-detector.ts
- Enhance format detection logic for better compatibility
- Update streaming utilities and chat completion handlers
- Improve model handling and route processing
- Build new Docker images with multi-architecture support
@fondoger
Copy link

In src/main.ts, you changed the default server from NodeJS to Bun. You also need to modify the file header from #!/usr/bin/env node to #!/usr/bin/env bun in order to get it work with npm install -g

image

@ericc-ch
Copy link
Owner

@fondoger I mean it says in the PR description that this is done by AI.

The changes are too much in terms of scope. They changed the package name as well.

But still thank you @nghyane , I may be able to use the code as reference. I also appreciate you wanting to contribute to this repo.

I've been wanting to try out Claude Code anyway.

@fondoger
Copy link

fondoger commented Jun 16, 2025

@ericc-ch Thank you very much for making this good project. I managed get ClaudeCode work with Github Copilot API use the instructions by #43 (comment).

image

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.

error in getTokenCount > countTokens
3 participants