kodeagent.kutils#
A minimal set of utilities used by KodeAgent. This module will be copied along with code for CodeAgent, so keep it minimum.
Auto-generate an OpenAI-style tool schema from a Python function. |
|
Call the LLM with the given parameters and response format. |
|
Clean and repair common JSON formatting issues from LLM responses. |
|
Combines consecutive user messages into a single message with a list of content items. |
|
Identify the content/MIME type of file pointed by a URL. |
|
Get a logger for KodeAgent. |
|
Identify whether a given MIME type is an image. |
|
Check whether a given path is a URL. |
|
Create a single user message to be sent to LiteLLM. |
|
Extract per-parameter descriptions from a docstring. |
|
Reads a prompt from the prompts directory. |
|
Validate that a provided chat history is OpenAI-compliant. |
A minimal set of utilities used by KodeAgent. This module will be copied along with code for CodeAgent, so keep it minimum.
- kodeagent.kutils.build_tool_schema(fn: Callable, just_first_line: bool = False, as_text: bool = True) dict[str, Any] | str[source]#
Auto-generate an OpenAI-style tool schema from a Python function.
- Parameters:
fn – The function to build a tool schema for.
just_first_line – Whether to use only the first line of the docstring for the description.
as_text – Whether to return the schema as a formatted text instead of a dictionary.
- Returns:
A dictionary representing the tool schema. Or a formatted text if as_text is True.
- async kodeagent.kutils.call_llm(model_name: str, litellm_params: dict, messages: list[dict], response_format: type[BaseModel] | None = None, trace_id: str | None = None, max_retries: int = 3, usage_tracker: Any | None = None, component_name: str = 'unknown') str | None[source]#
Call the LLM with the given parameters and response format.
- Parameters:
model_name – The name of the LLM model to use.
litellm_params – Dictionary of parameters to pass to litellm.
messages – List of message dictionaries.
response_format – Optional pydantic model for structured output.
trace_id – Optional trace ID for observability.
max_retries – Maximum number of retries for the LLM call.
usage_tracker – Optional UsageTracker instance to record usage.
component_name – Name of the component making the call (for tracking).
- Returns:
The LLM response as string.
- Raises:
RetryError – If the LLM call fails after maximum retries.
ValueError – If the LLM returns an empty or invalid response body.
- kodeagent.kutils.clean_json_string(json_str: str) str[source]#
Clean and repair common JSON formatting issues from LLM responses.
- Parameters:
json_str – Potentially malformed JSON string
- Returns:
Cleaned JSON string
- kodeagent.kutils.combine_user_messages(messages: list) list[source]#
Combines consecutive user messages into a single message with a list of content items.
- Returns:
A new list of messages with combined user messages.
- kodeagent.kutils.detect_file_type(url: str) str[source]#
Identify the content/MIME type of file pointed by a URL.
- Parameters:
url – The URL to the file.
- Returns:
The detected MIME type or Unknown file type.
- kodeagent.kutils.get_logger(name: str | None = 'KodeAgent') Logger[source]#
Get a logger for KodeAgent.
- Returns:
A logger instance.
- kodeagent.kutils.is_image_file(file_type) bool[source]#
Identify whether a given MIME type is an image.
- Parameters:
file_type – The file/content type.
- Returns:
True if an image file; False otherwise.
- kodeagent.kutils.is_it_url(path: str) bool[source]#
Check whether a given path is a URL.
- Parameters:
path – The path.
- Returns:
True if it’s a URL; False otherwise.
- kodeagent.kutils.make_user_message(text_content: str, files: list[str] | None = None) list[dict[str, Any]][source]#
Create a single user message to be sent to LiteLLM.
- Parameters:
text_content – The text content of the message.
files – An optional list of file paths or URLs, which can include images or other file types.
- Returns:
A list of dict items representing the messages.
- kodeagent.kutils.parse_param_descriptions(doc: str) dict[str, str][source]#
Extract per-parameter descriptions from a docstring.
Supports Google-style (Args:) and Sphinx-style (:param name:) formats.
- Parameters:
doc – The docstring to parse.
- Returns:
A dictionary mapping parameter names to their descriptions.
- kodeagent.kutils.read_prompt(filename: str) str[source]#
Reads a prompt from the prompts directory.
- Parameters:
filename – Name of the prompt file to read.
- Returns:
The content of the prompt file as a string.
- Raises:
FileNotFoundError – If the prompt file does not exist.
RuntimeError – If there is an error reading the file.
- kodeagent.kutils.validate_chat_history(history: list[dict], tool_names: set[str] | None = None) None[source]#
Validate that a provided chat history is OpenAI-compliant.
Performs stringent structural and semantic checks on each message. Raises
ValueErrorwith a precise description on the first problem found.- Parameters:
history – The chat history to validate. Must be a non-empty
list[dict].tool_names – Optional set of tool names registered on the agent. When provided, tool call names not in this set emit a
logger.warning.
- Raises:
ValueError – If the history fails any structural or compliance check.