Skip to content

Fix: Parse <tool_call> JSON for Kimi K2 + Add Kimi CLI OAuth support#3175

Closed
AIandI0x1 wants to merge 2 commits into
NousResearch:mainfrom
AIandI0x1:fix/kimi-tool-call-parsing
Closed

Fix: Parse <tool_call> JSON for Kimi K2 + Add Kimi CLI OAuth support#3175
AIandI0x1 wants to merge 2 commits into
NousResearch:mainfrom
AIandI0x1:fix/kimi-tool-call-parsing

Conversation

@AIandI0x1

Copy link
Copy Markdown
Contributor

Summary

This PR adds support for Kimi K2 models and improves authentication with Kimi CLI OAuth.

Changes

1. Fix Tool Call Parsing (agent/response_adapters.py)

Models like Kimi K2 output tool calls embedded in content:

<tool_call>{"name": "search_files", "arguments": {...}}</tool_call>

Previously these were displayed raw to users instead of being executed. Now they're properly parsed and executed.

Changes:

  • Add agent/response_adapters.py with adapt_hermes_tool_call_response() function
  • Reuses existing _parse_hermes_json_tool_calls() logic
  • Apply adapter in main agent loop when <tool_call> detected in content

2. Add Kimi CLI OAuth Support (hermes_cli/auth.py, agent/auxiliary_client.py)

Users who have authenticated with the official Kimi CLI can now use those same credentials without separate API keys.

Changes:

  • Add _read_kimi_cli_oauth_credentials() to read ~/.kimi/credentials/kimi-code.json
  • Add _get_kimi_cli_headers() to generate proper headers for OAuth requests
  • Add _get_kimi_cli_device_id() helper
  • Update _resolve_api_key_provider_secret() to check for Kimi CLI OAuth first
  • Update resolve_api_key_provider_credentials() to return default_headers
  • Update AIAgent to use OAuth headers when available

Testing

  • Tested with kimi-k2.5 model
  • Tool calls are now correctly extracted and executed
  • OAuth authentication works with existing Kimi CLI credentials

Backward Compatibility

  • Non-breaking: OAuth is optional
  • Existing API key auth continues to work
  • Falls back to simple User-Agent header if OAuth not available

Models like Kimi K2 output tool calls embedded in content as:
<tool_call>{"name": "tool_name", "arguments": {...}}</tool_call>

This was being displayed raw instead of being parsed and executed.

Changes:
- Add agent/response_adapters.py with adapt_hermes_tool_call_response() to parse
  Hermes-style <tool_call> JSON format
- Apply adapter in main agent loop when <tool_call> detected in content
- Reuses existing _parse_hermes_json_tool_calls() logic

Fix is model-agnostic: works for any model outputting this format.
Fixes issue where tool calls from kimi-coding provider were not executed.
Add support for using Kimi CLI OAuth credentials to authenticate
with the kimi-coding provider (api.kimi.com).

This allows users who have authenticated with the official Kimi CLI
to use those same credentials in hermes-agent without separate API keys.

Changes:
- Add _read_kimi_cli_oauth_credentials() to read ~/.kimi/credentials/kimi-code.json
- Add _get_kimi_cli_headers() to generate proper headers for OAuth requests
- Add _get_kimi_cli_device_id() helper
- Update _resolve_api_key_provider_secret() to check for Kimi CLI OAuth first
- Update resolve_api_key_provider_credentials() to return default_headers
- Update AIAgent to use OAuth headers when available

Non-breaking: Falls back to simple User-Agent header if OAuth not available.
@AIandI0x1 AIandI0x1 closed this by deleting the head repository Apr 22, 2026
@alt-glitch alt-glitch added type/feature New feature or request P3 Low — cosmetic, nice to have provider/kimi Kimi / Moonshot comp/agent Core agent loop, run_agent.py, prompt builder comp/cli CLI entry point, hermes_cli/, setup wizard area/auth Authentication, OAuth, credential pools labels Apr 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/auth Authentication, OAuth, credential pools comp/agent Core agent loop, run_agent.py, prompt builder comp/cli CLI entry point, hermes_cli/, setup wizard P3 Low — cosmetic, nice to have provider/kimi Kimi / Moonshot type/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants