fix: health checker IDC support + compaction error fix + OpenAI error format + code optimization#38
Open
poboll wants to merge 9 commits intoaliom-v:mainfrom
Open
fix: health checker IDC support + compaction error fix + OpenAI error format + code optimization#38poboll wants to merge 9 commits intoaliom-v:mainfrom
poboll wants to merge 9 commits intoaliom-v:mainfrom
Conversation
- Add client_id and client_secret support to health checker for IDC tokens - Check both active and invalid tokens during health checks - Automatically restore valid tokens to 'active' status - Add new Claude 4.6 models (opus/sonnet and thinking variants) - Add missing inject_output_limit_warning config setting Fixes: Token health checker incorrectly marks IDC tokens as invalid
- Add validate_and_fix_tool_pairs function to check for orphan toolUses - Remove toolUses without matching toolResults to prevent 'Improperly formed request' error - This fixes the issue when OpenCode compaction removes toolResults but leaves toolUses
- Fix AttributeError when tool_calls contains dict instead of objects - Use hasattr and isinstance to handle both formats
749e92a to
10ff82e
Compare
…licate tool ID extraction - trim_history_to_fit: pre-compute sizes array to avoid O(n²) recalculation - Extract _split_last_message_tool_results from build_kiro_payload (30+ lines → clean function) - validate_and_fix_tool_pairs: extract _get_tc_id helper, merge collection passes - Fix tool_results/text split in last message for Kiro API compatibility
…plicate imports - Extract CONTENT_LENGTH_EXCEEDS_THRESHOLD and Improperly formed request retry into helper function - Remove duplicate 'from kiro_gateway.config import settings' import - Remove unused reduced_threshold variable - Eliminate redundant response.aclose() and retry call patterns
- Add http_exception_handler for OpenAI SDK compatibility (OpenCode, OpenClaw) - Map HTTP status codes to OpenAI error types (authentication_error, rate_limit_error, etc.) - Update validation_exception_handler to also return OpenAI format for /v1/ endpoints - Register handler in main.py, update __init__.py exports
- Add __pycache__/, *.pyc, *.pyo for Python cache - Add debug_logs/ for debug output - Add kirogate-progress.md for local notes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
本 PR 包含 5 大改动:健康检查器 IDC 修复、工具配对验证修复、OpenAI 兼容错误格式、请求重试机制、代码优化重构。
Changes
1. Health Checker IDC Token Support (
health_checker.py)client_id和client_secret以支持 IDC token 刷新2. Tool Pairs 验证修复 (
converters.py)问题: OpenCode 执行 compaction(上下文裁剪)后,历史消息中可能出现:
toolUses但对应的 usertoolResults被删除toolResults但对应的 assistanttoolUses被删除Kiro API 要求每个 toolUse 必须有对应的 toolResult,否则返回
"Improperly formed request"。修复:
validate_and_fix_tool_pairs()— 双向验证,移除所有孤立的 tool_calls 和 tool_results_split_last_message_tool_results()— 当最后一条消息同时包含 tool_results 和文本时,将 tool_results 拆分到历史中(与 toolUse 配对),文本作为 currentMessagetrim_history_to_fit()— 历史消息过长时自动裁剪,保持 tool_use/tool_result 配对完整build_kiro_payload()调用顺序:validate → trim → merge → split3. OpenAI 兼容错误格式 (
exceptions.py,main.py)问题: FastAPI 默认的
HTTPException返回{"detail": "..."}格式,但 OpenCode (@ai-sdk/openai-compatible) 和 OpenClaw (openai-completions模式) 都期望 OpenAI 标准格式:{"error": {"message": "...", "type": "...", "code": ...}}修复:
http_exception_handler()— 对所有/v1/端点的错误返回 OpenAI 格式validation_exception_handler()也对/v1/端点返回 OpenAI 格式main.py注册全局异常处理器4. 请求重试机制 (
request_handler.py)CONTENT_LENGTH_EXCEEDS_THRESHOLD错误:自动将历史裁剪到 1/3 后重试Improperly formed request错误:移除全部历史和孤立 toolResults 后重试_maybe_build_retry_payload()函数5. 代码优化重构
converters.py:
trim_history_to_fit(): 从 O(n²) 优化到 O(n),预计算 sizes 数组做增量减法validate_and_fix_tool_pairs(): 提取_get_tc_id()消除重复的 ID 提取逻辑,合并两个收集 passbuild_kiro_payload(): 30+ 行的 split 逻辑提取为独立的_split_last_message_tool_results()函数request_handler.py:
_maybe_build_retry_payload()函数from kiro_gateway.config import settings重复导入reduced_threshold变量6. 其他
.gitignore添加kiro_creds.jsonFiles Changed
kiro_gateway/converters.pykiro_gateway/health_checker.pykiro_gateway/exceptions.pykiro_gateway/request_handler.pykiro_gateway/config.pydocker-compose.ymlmain.pykiro_gateway/__init__.py.gitignoreTesting
基础请求测试
Tool Calls 测试(验证 tool pairs 修复)
错误格式测试(验证 OpenAI 兼容)
Anthropic 格式测试
所有测试均已通过验证。