# -*- coding: utf-8 -*- # Kiro Gateway # https://github.com/jwadow/kiro-gateway # Copyright (C) 2025 Jwadow # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . """ Utility functions for Kiro Gateway. Contains functions for fingerprint generation, header formatting, and other common utilities. """ import hashlib import uuid from typing import TYPE_CHECKING from loguru import logger if TYPE_CHECKING: from kiro.auth import KiroAuthManager def get_machine_fingerprint() -> str: """ Generates a unique machine fingerprint based on hostname and username. Used for User-Agent formation to identify a specific gateway installation. Returns: SHA256 hash of the string "{hostname}-{username}-kiro-gateway" """ try: import socket import getpass hostname = socket.gethostname() username = getpass.getuser() unique_string = f"{hostname}-{username}-kiro-gateway" return hashlib.sha256(unique_string.encode()).hexdigest() except Exception as e: logger.warning(f"Failed to get machine fingerprint: {e}") return hashlib.sha256(b"default-kiro-gateway").hexdigest() def get_kiro_headers(auth_manager: "KiroAuthManager", token: str) -> dict: """ Builds headers for Kiro API requests. Includes all necessary headers for authentication and identification: - Authorization with Bearer token - User-Agent with fingerprint - AWS CodeWhisperer specific headers Args: auth_manager: Authentication manager for obtaining fingerprint token: Access token for authorization Returns: Dictionary with headers for HTTP request """ fingerprint = auth_manager.fingerprint return { "Authorization": f"Bearer {token}", "Content-Type": "application/json", "User-Agent": f"aws-sdk-js/1.0.27 ua/2.1 os/win32#10.0.19044 lang/js md/nodejs#22.21.1 api/codewhispererstreaming#1.0.27 m/E KiroIDE-0.7.45-{fingerprint}", "x-amz-user-agent": f"aws-sdk-js/1.0.27 KiroIDE-0.7.45-{fingerprint}", "x-amzn-codewhisperer-optout": "true", "x-amzn-kiro-agent-mode": "vibe", "amz-sdk-invocation-id": str(uuid.uuid4()), "amz-sdk-request": "attempt=1; max=3", } def generate_completion_id() -> str: """ Generates a unique ID for chat completion. Returns: ID in format "chatcmpl-{uuid_hex}" """ return f"chatcmpl-{uuid.uuid4().hex}" def generate_conversation_id() -> str: """ Generates a unique ID for conversation. Returns: UUID in string format """ return str(uuid.uuid4()) def generate_tool_call_id() -> str: """ Generates a unique ID for tool call. Returns: ID in format "call_{uuid_hex[:8]}" """ return f"call_{uuid.uuid4().hex[:8]}"