Skip to content

Add Kiro CLI provider with multi-credential support#116

Open
ItzMeAyben wants to merge 3 commits intoMirrowel:mainfrom
ItzMeAyben:feature/kiro-cli-provider
Open

Add Kiro CLI provider with multi-credential support#116
ItzMeAyben wants to merge 3 commits intoMirrowel:mainfrom
ItzMeAyben:feature/kiro-cli-provider

Conversation

@ItzMeAyben
Copy link

Summary

Add Kiro CLI (Amazon Q / Kiro) provider enabling access to Claude models via AWS Bedrock using Kiro CLI authentication.

Port Note: Uses 7777 (project default) vs reference's 8000.

Features

  • Multi-credential authentication - Environment variables, JSON files, SQLite database with auto-detection
  • Automatic token refresh - Refreshes expired tokens via AWS SSO OIDC/Kiro Desktop endpoints automatically
  • Token persistence - Saves refreshed tokens back to source (JSON/SQLite)
  • Dual authentication types - Kiro Desktop and AWS SSO OIDC (auto-detected)
  • AWS Event Stream parsing - Proper handling of streaming responses with <thinking> block extraction
  • Full OpenAI compatibility - Chat completions, streaming, tool calling, vision support
  • Cross-platform - macOS and Linux path support

Supported Models

All Claude models available through Kiro API:

Model Thinking Support Context Limit
claude-sonnet-4-5 200K
claude-opus-4-5 200K
claude-sonnet-4 200K
claude-haiku-4-5 200K
claude-3-7-sonnet 200K

Architecture

Authentication Flow

sequenceDiagram
    participant Client as API Client
    participant Proxy as LLM Proxy
    participant Auth as KiroAuthManager
    participant Store as Credential Store
    participant OIDC as AWS SSO/Kiro Auth
    participant API as Kiro Bedrock API

    Client->>Proxy: POST /v1/chat/completions
    Proxy->>Auth: get_access_token()
    Auth->>Store: Load credentials
    Store-->>Auth: Token + metadata
    
    alt Token valid (> 10min remaining)
        Auth-->>Proxy: Valid access_token
    else Token expired or expiring
        Auth->>OIDC: Refresh token request
        OIDC-->>Auth: New access_token (1hr)
        Auth->>Store: Save refreshed token
        Auth-->>Proxy: New access_token
    end
    
    Proxy->>API: Request with Bearer token
    API-->>Proxy: Event Stream response
    Proxy-->>Client: OpenAI SSE chunks
Loading

Token Management

Automatic Refresh

  • Access tokens expire in 1 hour
  • Tokens automatically refresh 10 minutes before expiry
  • Refreshed tokens saved back to credential source
  • Supports both Kiro Desktop and AWS SSO OIDC flows

Token Storage Locations

OS Path
macOS ~/Library/Application Support/kiro-cli/data.sqlite3
macOS ~/.aws/sso/cache/kiro-auth-token.json
Linux ~/.local/share/kiro-cli/data.sqlite3
Linux ~/.aws/sso/cache/kiro-auth-token.json

Authentication Priority

  1. KIRO_REFRESH_TOKEN / REFRESH_TOKEN environment variable
  2. JSON credentials file (KIRO_CREDS_FILE)
  3. SQLite database (KIRO_CLI_DB_FILE)
  4. Auto-detected default paths

System Architecture

graph TB
    subgraph "LLM Proxy Server"
        A[FastAPI Router] --> B[KiroCliProvider]
        B --> C[Message Converters]
        B --> D[Stream Parser]
        B --> E[HTTP Client]
    end
    
    subgraph "Authentication Layer"
        F[KiroAuthManager] --> G[(JSON Files)]
        F --> H[(SQLite DB)]
        F --> I[Token Refresh]
        I --> J[AWS SSO OIDC]
        I --> K[Kiro Desktop Auth]
    end
    
    subgraph "External Services"
        L[Kiro Bedrock API]
    end
    
    B --> F
    E -->|Bearer Token| L
    F -->|Credentials| E
    L -->|AWS Event Stream| D
    D -->|OpenAI SSE| A
    C -->|Kiro Payload| E
Loading

Project Structure

src/
└── rotator_library/                    # Core library (LGPL-3.0-only)
    ├── credential_manager.py           # Modified: Kiro credential discovery
    └── providers/                      # LLM provider implementations
        ├── kiro_auth_base.py           # NEW: Token lifecycle & auth manager
        ├── kiro_cli_provider.py        # NEW: Provider implementation
        └── utilities/
            ├── kiro_converters.py      # NEW: Message/tool format conversion
            ├── kiro_http_client.py     # NEW: HTTP client with retry logic
            ├── kiro_streaming.py       # NEW: AWS Event Stream parser
            └── kiro_utils.py           # NEW: Configuration & utilities

src/proxy_app/                          # Proxy application (MIT)
├── launcher_tui.py                     # Modified: TUI credential detection
└── main.py                             # Modified: Skip JSON parsing for SQLite

docker/
├── docker-compose.dev.yml              # Modified: Kiro credential volume mounts
└── .env.example                        # Modified: Kiro auth documentation

Testing

Verified working across multiple scenarios:

  • ✅ JSON credentials (Kiro IDE, macOS)
  • ✅ SQLite credentials (kiro-cli, macOS)
  • ✅ Environment variable authentication
  • ✅ Auto-detection across credential types
  • ✅ Token refresh and persistence
  • ✅ Chat completions endpoint
  • ✅ Streaming responses with thinking blocks
  • ✅ Tool calling support
  • ✅ Vision support
  • ✅ Multi-turn conversations
  • ✅ Model listing
  • ✅ Docker deployment with volume mounts
  • ✅ Cross-platform paths (macOS/Linux)

Implementation Notes

  • Provider Interface: Follows ProviderInterface pattern from provider_interface.py
  • Custom Logic: Implements has_custom_logic() for direct API handling
  • Licensing: LGPL-3.0 headers on all new files (per CONTRIBUTING.md)
  • Integration: Seamlessly integrated with existing credential system
  • Documentation: Comprehensive setup guide in README.md & .env.example
  • Error Handling: Exponential backoff for 403/429/5xx with automatic token refresh on 403

Checklist

  • Code follows project's style guidelines
  • LGPL-3.0 license headers added to all new files
  • Follows ProviderInterface pattern
  • Integrated with existing credential management
  • Cross-platform support (macOS/Linux)
  • Documentation added (README.md, .env.example)
  • Docker deployment support
  • Tested with multiple authentication methods

Acknowledgments

Implementation adapted from kiro-gateway by @Jwadow - reference for Kiro API flows, AWS Event Stream parsing, and message format conversions. Integrated into LLM-API-Key-Proxy's provider architecture with LGPL-3.0 licensing.

ItzMeAyben and others added 3 commits February 2, 2026 23:04
Add comprehensive Kiro CLI (Amazon Q / Kiro) provider implementation with support for multiple authentication methods:

- Direct refresh token via environment variables
- JSON credentials from Kiro IDE (~/.aws/sso/cache/kiro-auth-token.json)
- SQLite database from kiro-cli (macOS and Linux)
- Auto-detection with configurable priority

Features:
- KiroAuthManager with AWS SSO OIDC and Kiro Desktop auth support
- Token refresh with automatic expiration handling
- HTTP client with retry logic (403, 429, 5xx)
- OpenAI message/tool format conversion to Kiro API
- AWS Event Stream parsing for streaming responses
- Thinking block extraction and formatting
- macOS and Linux path support

Updates:
- credential_manager.py: Add Kiro credential discovery
- launcher_tui.py: Add Kiro credential detection in TUI
- main.py: Skip JSON parsing for Kiro SQLite credentials
- docker-compose.dev.yml: Add Kiro credential volume mounts
- .env.example: Document all Kiro auth options
- README.md: Add comprehensive Kiro setup documentation

Co-authored-by: Cursor <cursoragent@cursor.com>
Add SPDX-License-Identifier and Copyright headers to all new Kiro provider
files as required by CONTRIBUTING.md guidelines.

Co-authored-by: Cursor <cursoragent@cursor.com>
@ItzMeAyben ItzMeAyben requested a review from Mirrowel as a code owner February 2, 2026 18:48
Repository owner deleted a comment from mirrobot-agent bot Mar 3, 2026
@Mirrowel
Copy link
Owner

Mirrowel commented Mar 3, 2026

@greptile

@Mirrowel
Copy link
Owner

Mirrowel commented Mar 3, 2026

@ItzMeAyben I didn't notice before, but the base branch here is main - it should be Dev branch instead. Main is never directly modified and is outdated.

@Mirrowel Mirrowel added enhancement New feature or request Agent Monitored Monitored for AI Agent to review PR's and commits Priority labels Mar 3, 2026
@greptile-apps
Copy link

greptile-apps bot commented Mar 3, 2026

Greptile Summary

This PR adds comprehensive Kiro CLI provider support, enabling access to Claude models via AWS Bedrock using Kiro CLI/IDE authentication. The implementation is well-architected and follows the existing provider interface pattern.

Key Changes:

  • Implemented complete authentication system supporting three credential sources: environment variables, JSON files (Kiro IDE), and SQLite databases (kiro-cli)
  • Built automatic credential refresh with 10-minute safety margin, supporting both Kiro Desktop and AWS SSO OIDC auth flows
  • Created AWS Event Stream parser with thinking block extraction and OpenAI SSE conversion
  • Added message/tool format converters with schema sanitization for API compatibility
  • Updated default port from 8000 to 7777 across all configurations and documentation
  • Integrated credential auto-detection into existing discovery system

Implementation Quality:

  • All new files include proper LGPL-3.0 license headers as required
  • Follows established ProviderInterface pattern with custom logic support
  • Comprehensive error handling with exponential backoff and automatic retry
  • Thread-safe token refresh using asyncio locks to prevent race conditions
  • Cross-platform support with proper path handling for macOS and Linux
  • Extensive documentation in README and .env.example

Testing Coverage:
The PR description indicates thorough testing across multiple authentication methods, API features (streaming, tools, vision), and deployment scenarios (local and Docker).

Minor Observations:

  • Two minor style improvements suggested in inline comments
  • Otherwise, code quality is high with proper error handling, parameterized SQL queries, and defensive programming

Confidence Score: 5/5

  • This PR is safe to merge - well-structured implementation with proper error handling and comprehensive testing
  • Score reflects excellent code quality: follows established patterns, includes proper licensing, has comprehensive error handling, uses parameterized SQL queries (no injection risk), implements thread-safe token refresh, and includes extensive documentation. Only minor style suggestions identified.
  • No files require special attention - all implementations follow best practices

Important Files Changed

Filename Overview
src/rotator_library/providers/kiro_auth_base.py Core authentication manager with token lifecycle, credential loading (JSON/SQLite), automatic refresh, and multi-auth support (Kiro Desktop/AWS SSO OIDC)
src/rotator_library/providers/kiro_cli_provider.py Provider implementation following ProviderInterface pattern, handles model resolution, dynamic model discovery, and streaming/non-streaming responses
src/rotator_library/providers/utilities/kiro_converters.py Message format conversions between OpenAI and Kiro APIs, handles tools, images, thinking tags injection, and schema sanitization for compatibility
src/rotator_library/providers/utilities/kiro_streaming.py AWS Event Stream parser with thinking block extraction, tool call parsing, OpenAI SSE conversion, handles first token timeout and retry logic
src/rotator_library/credential_manager.py Added Kiro CLI credential discovery supporting env tokens, JSON files, and SQLite databases with priority-based detection across platforms
src/proxy_app/main.py Changed default port from 8000 to 7777, added special handling to skip JSON parsing for kiro_cli SQLite credentials during initialization
README.md Added comprehensive Kiro CLI documentation covering three authentication methods, auto-detection paths, configuration options, and examples

Sequence Diagram

sequenceDiagram
    participant Client as API Client
    participant Proxy as LLM Proxy
    participant Provider as KiroCliProvider
    participant Auth as KiroAuthManager
    participant Store as Credential Store<br/>(JSON/SQLite)
    participant OIDC as AWS SSO/Kiro Auth
    participant API as Kiro Bedrock API

    Client->>Proxy: POST /v1/chat/completions<br/>model: kiro_cli/claude-sonnet-4.5
    Proxy->>Provider: acompletion(messages, tools)
    Provider->>Auth: Request authentication
    
    alt Credentials valid (more than 10min remaining)
        Auth-->>Provider: Return valid auth header
    else Credentials expired or expiring soon
        Auth->>Store: Reload from disk
        Store-->>Auth: Latest credentials with metadata
        
        alt Still expired after reload
            Auth->>OIDC: POST refresh request
            OIDC-->>Auth: New credentials with expiry
            Auth->>Store: Persist updated credentials
        end
        
        Auth-->>Provider: Return fresh auth header
    end
    
    Provider->>Provider: Convert OpenAI to Kiro format
    Provider->>API: POST /generateAssistantResponse<br/>with Bearer authorization
    
    alt Streaming enabled
        API-->>Provider: AWS Event Stream
        Provider->>Provider: Parse events, extract thinking
        Provider-->>Client: OpenAI SSE chunks
    else Non-streaming
        API-->>Provider: AWS Event Stream
        Provider->>Provider: Collect full response
        Provider-->>Client: OpenAI completion
    end
Loading

Last reviewed commit: 076746c

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

23 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

await self.auth_manager.force_refresh()
continue

if response.status_code in (429,) or 500 <= response.status_code < 600:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single-element tuple (429,) is unnecessarily verbose

Suggested change
if response.status_code in (429,) or 500 <= response.status_code < 600:
if response.status_code == 429 or 500 <= response.status_code < 600:
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/rotator_library/providers/utilities/kiro_http_client.py
Line: 96

Comment:
Single-element tuple `(429,)` is unnecessarily verbose

```suggestion
                if response.status_code == 429 or 500 <= response.status_code < 600:
```

How can I resolve this? If you propose a fix, please make it concise.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!


# Special-case: Kiro CLI supports multiple credential sources
# Priority: REFRESH_TOKEN (env) > KIRO_CREDS_FILE (JSON) > KIRO_CLI_DB_FILE (SQLite) > default paths
kiro_refresh_token = self.env_vars.get("KIRO_REFRESH_TOKEN") or self.env_vars.get("REFRESH_TOKEN")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using only KIRO_REFRESH_TOKEN instead of falling back to generic REFRESH_TOKEN to avoid potential conflicts with other systems

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/rotator_library/credential_manager.py
Line: 129

Comment:
Consider using only `KIRO_REFRESH_TOKEN` instead of falling back to generic `REFRESH_TOKEN` to avoid potential conflicts with other systems

How can I resolve this? If you propose a fix, please make it concise.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@mirrobot-agent
Copy link
Contributor

mirrobot-agent bot commented Mar 3, 2026

Starting my review of this Kiro CLI provider implementation. Adding support for Claude models via AWS Bedrock with multi-credential authentication - this looks comprehensive! Analyzing the new provider files now. 🔍

Copy link
Contributor

@mirrobot-agent mirrobot-agent bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall Assessment

Excellent PR implementing comprehensive Kiro CLI provider support! The architecture is well-designed with clean separation of concerns across authentication, HTTP client, streaming, and conversion layers. The code follows the existing provider interface pattern and includes proper LGPL-3.0 licensing on all new files.

Key Strengths

  • Multi-credential authentication: Clean implementation supporting environment variables, JSON files, and SQLite databases with auto-detection
  • Token lifecycle management: Proper refresh handling with 10-minute safety margin and persistent storage
  • AWS Event Stream parsing: Well-implemented parser for streaming responses with thinking block extraction
  • Error handling: Good use of exponential backoff and automatic token refresh on 403 errors
  • Thread safety: Proper use of asyncio.Lock for token refresh to prevent race conditions

Notes

I concur with Greptile's observations regarding:

  • The verbose tuple syntax in kiro_http_client.py:96 could be simplified
  • The REFRESH_TOKEN fallback in credential_manager.py:129 could conflict with other systems

Minor Suggestion

The silent JSON parsing failure in extract_tool_uses_from_message() could mask issues with malformed tool arguments. Consider adding logging when json.loads() fails.

Questions for the Author

  1. Has this been tested with both Kiro Desktop and AWS SSO OIDC authentication flows in production?
  2. Are there any plans to support Windows paths for the SQLite auto-detection?

Overall, this is high-quality code ready for merge after addressing the minor suggestions.

This review was generated by an AI assistant.

Comment on lines +273 to +275
func = tc.get("function", {})
arguments = func.get("arguments", "{}")
if isinstance(arguments, str):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If json.loads() fails here, input_data becomes an empty dict but the code continues silently. Consider logging this parsing failure so malformed tool arguments don't go unnoticed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Agent Monitored Monitored for AI Agent to review PR's and commits enhancement New feature or request Priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants