lwe.core Package¶
lwe.core Submodules¶
lwe.core.config module¶
lwe.core.constants module¶
lwe.core.doc_parser module¶
lwe.core.editor module¶
- lwe.core.editor.discover_editor()[source]¶
Discovers and returns the appropriate editor command as a list of arguments.
Handles cases where the editor command includes arguments, including quoted arguments with spaces (e.g. ‘vim -c “set noswapfile”’).
- Returns:
A list of command parts ready for subprocess execution
- Return type:
list[str]
- lwe.core.editor.file_editor(filepath)[source]¶
Opens the specified file in the system’s configured editor.
- Parameters:
filepath (str) – Path to the file to edit
- lwe.core.editor.get_environment_editor(default=None)[source]¶
Fetches the preferred editor from the environment variables.
This function checks the following environment variables in order to determine the user’s preferred editor:
LWE_EDITOR
VISUAL
EDITOR
- Parameters:
default (str or None) – The default editor to return if no environment variable is set.
- Returns:
The preferred editor as specified by environment variables or the default value.
- Return type:
str or None
- lwe.core.editor.pipe_editor(input_data='', suffix=None)[source]¶
Opens the system editor with optional input data and returns the edited content.
This function creates a temporary file with the provided input data, opens it in the system editor, waits for the user to make changes and close the editor, then reads and returns the modified content. The temporary file is deleted afterwards.
- Parameters:
input_data (str) – Initial content to populate the editor with
suffix (str or None) – Optional file extension for the temporary file (e.g. ‘.txt’, ‘.md’)
- Returns:
The edited content after the editor is closed
- Return type:
str
lwe.core.error module¶
lwe.core.tool module¶
lwe.core.tool_cache module¶
lwe.core.tool_manager module¶
lwe.core.logger module¶
lwe.core.monkey_patch module¶
lwe.core.plugin module¶
- class lwe.core.plugin.Plugin(config=None, cache_manager=None)[source]¶
Bases:
PluginBase,ABC
lwe.core.plugin_manager module¶
lwe.core.preset_manager module¶
lwe.core.provider module¶
- class lwe.core.provider.PresetValue(value_type, min_value=None, max_value=None, options=None, include_none=False, private=False)[source]¶
Bases:
object
- class lwe.core.provider.Provider(config=None, cache_manager=None)[source]¶
Bases:
ProviderBase- abstract property capabilities¶
- abstract property default_model¶
- class lwe.core.provider.ProviderBase(config=None, cache_manager=None)[source]¶
Bases:
Plugin- property available_models¶
- convert_ai_dict_to_message(message: Mapping[str, Any]) AIMessage[source]¶
Convert an LWE message dictionary to a LangChain AIMessage.
This default implementation supports a format suitable for OpenAI. Other providers plugins may need to override this method depending on the structure of their AI messages.
- convert_dict_to_message(message: Mapping[str, Any]) BaseMessage[source]¶
Convert an LWE message dictionary to a LangChain message.
- property display_name¶
- property model_property_name¶
- property plugin_type¶
- property static_models¶
lwe.core.provider_manager module¶
lwe.core.repl module¶
- class lwe.core.repl.Repl(config=None)[source]¶
Bases:
objectA shell interpreter that serves as a front end to the backend classes
- action_template_copy(*template_names)[source]¶
Copies an existing template and saves it as a new template.
- Parameters:
template_names (tuple) – The names of the old and new templates.
- Returns:
Success status, new file path, and user message.
- Return type:
tuple
- action_template_delete(template_name)[source]¶
Deletes an existing template.
- Parameters:
template_name (str) – The name of the template to delete.
- action_template_edit(template_name)[source]¶
Create a new template, or edit an existing template.
- Parameters:
template_name (str) – The name of the template.
- action_template_edit_run(template_name)[source]¶
Open a template for final editing, then run it.
- Parameters:
template_name (str) – The name of the template.
- action_template_prompt_edit_run(template_name)[source]¶
Prompts for a value for each variable in the template, sustitutes the values in the template, opens an editor for final edits, and sends the final content to the model as your input.
- Parameters:
template_name (str) – The name of the template.
- action_template_prompt_run(template_name)[source]¶
Prompt for template variable values, then run.
- Parameters:
template_name (str) – The name of the template.
- action_template_run(template_name)[source]¶
Run a template.
- Parameters:
template_name (str) – The name of the template.
- action_template_show(template_name)[source]¶
Display a template.
- Parameters:
template_name (str) – The name of the template.
- command_ask(input)[source]¶
Ask a question
It is purely optional.
- Examples:
{COMMAND} what is 6+6 (is the same as ‘what is 6+6’)
- command_chat(arg)[source]¶
Retrieve chat content
- Arguments:
history_id: The history ID With no arguments, show content of the current conversation.
- Examples:
Current conversation: {COMMAND} Older conversation: {COMMAND} 2
- command_config(arg)[source]¶
Show or edit the current configuration
- Examples:
Show all: {COMMAND} Edit config: {COMMAND} edit Show files config: {COMMAND} files Show profile config: {COMMAND} profile Show runtime config: {COMMAND} runtime Show section: {COMMAND} debug
- command_delete(arg)[source]¶
Delete one or more conversations
Can delete by conversation ID, history ID, or current conversation.
- Arguments:
history_id : The history ID
Arguments can be mixed and matched as in the examples below.
- Examples:
Current conversation: {COMMAND} Delete one: {COMMAND} 3 Multiple IDs: {COMMAND} 1,5 Ranges: {COMMAND} 1-5 Complex: {COMMAND} 1,3-5
- command_editor(args)[source]¶
Open an editor for entering a command
When the editor is closed, the content is sent.
- Arguments:
default_text: The default text to open the editor with
- Examples:
{COMMAND} {COMMAND} some text to start with
- command_file(arg)[source]¶
Send a prompt read from the named file
- Arguments:
file_name: The name of the file to read from
- Examples:
{COMMAND} myprompt.txt
- command_history(arg)[source]¶
Show recent conversation history
- Arguments;
limit: limit the number of messages to show (default {DEFAULT_HISTORY_LIMIT}) offset: offset the list of messages by this number
- Examples:
{COMMAND} {COMMAND} 10 {COMMAND} 10 5
- command_log(arg)[source]¶
Enable/disable logging to a file
- Arguments:
file_name: The name of the file to write to
- Examples:
Log to file: {COMMAND} mylog.txt Disable logging: {COMMAND}
- command_model(arg)[source]¶
View or set attributes on the current LLM model
- Arguments:
path: The attribute path to view or set value: The value to set the attribute to With no arguments, view current set model attributes
- Examples:
{COMMAND} {COMMAND} temperature {COMMAND} temperature 1.1
- command_plugin(args)[source]¶
Perform operations on plugins.
- Arguments:
action: The action to perform. One of: reload target: The target for the action.
- Examples:
{COMMAND} reload echo {COMMAND} reload provider_chat_openai
- command_plugins(arg)[source]¶
List installed plugins
Plugins are enabled by adding their name to the list of enabled plugins in the profile configuration.
- Arguments:
filter_string: Optional. String to filter plugins by. Name and description are matched.
- Examples:
{COMMAND} {COMMAND} shell
- command_read(_)[source]¶
Begin reading multi-line input
Allows for entering more complex multi-line input prior to sending it.
- Examples:
{COMMAND}
- command_stream(_)[source]¶
Toggle streaming mode
Streaming mode: streams the raw response (no markdown rendering) Non-streaming mode: Returns full response at completion (markdown rendering supported).
- Examples:
{COMMAND}
- command_switch(arg)[source]¶
Switch to chat
- Arguments:
history_id: The history ID of the conversation
- Examples:
{COMMAND} 2
- command_template(args)[source]¶
Run actions on available templates
Templates are pre-configured text content that can be customized before sending a message to the model.
‘Running’ a template sends its content (after variable substitutions) to the model as your input.
- Available actions:
copy: Copy a template
delete: Delete a template
edit: Open or create a template for editing
edit-run: Open the template in an editor, then run it on editor save and close.
prompt-edit-run: Collect values for template variables, then open in an editor, then run it on editor save and close
prompt-run: Collect values for template variables, then run it
run: Run a template
show: Show a template
- Arguments:
template_name: Required. The name of the template.
For copy, a new template name is also required.
- Examples:
/template copy mytemplate.md mytemplate_copy.md
/template delete mytemplate.md
/template edit mytemplate.md
/template edit-run mytemplate.md
/template prompt-edit-run mytemplate.md
/template prompt-run mytemplate.md
/template run mytemplate.md
/template show mytemplate.md
- command_templates(arg)[source]¶
List available templates
Templates are pre-configured text content that can be customized before sending a message to the model.
They are located in the ‘templates’ directory in the following locations:
The main configuration directory
The profile configuration directory
See {COMMAND_LEADER}config for current locations.
- Arguments:
filter_string: Optional. If provided, only templates with a name or description containing the filter string will be shown.
- Examples:
{COMMAND} {COMMAND} filterstring
- command_title(arg)[source]¶
Show or set title
- Arguments:
title: title of the current conversation …or… history_id: history ID of conversation
- Examples:
Get current conversation title: {COMMAND} Set current conversation title: {COMMAND} new title Set conversation title using history ID: {COMMAND} 1
- doc_header = 'Documented commands type /help [command without /] (e.g. /help ask) for detailed help'¶
- intro = 'Provide a prompt, or type /help or ? to list commands.'¶
- logfile = None¶
- message_map = {}¶
- prompt = '> '¶
- prompt_number = 0¶
- prompt_prefix = ''¶
lwe.core.template_manager module¶
- class lwe.core.template_manager.TemplateManager(config=None)[source]¶
Bases:
objectManage templates.
- build_message_from_template(template_name, substitutions=None)[source]¶
Build a message from a given template and substitutions.
- Parameters:
template_name (str) – The name of the template to use.
substitutions (dict, optional) – The substitutions to apply to the template. Defaults to None.
- Returns:
The rendered message and any overrides.
- Return type:
tuple
- copy_template(old_name, new_name)[source]¶
Copies a template file to a new location.
- Parameters:
old_name (str) – The name of the existing template file.
new_name (str) – The name for the new template file.
- Returns:
A tuple containing a boolean indicating success or failure, the new file path, and a status message.
- Return type:
tuple
- ensure_template(template_name)[source]¶
Checks if a template exists.
- Parameters:
template_name (str) – The name of the template to check.
- Returns:
A tuple containing a boolean indicating if the template exists, the template name, and a message.
- Return type:
tuple
- extract_metadata_keys(keys, metadata)[source]¶
Extracts specified keys from the metadata.
- Parameters:
keys (list) – Keys to be extracted from the metadata.
metadata (dict) – The metadata from which keys are to be extracted.
- Returns:
A tuple containing the updated metadata and the extracted keys.
- Return type:
tuple
- extract_template_run_overrides(metadata)[source]¶
Extracts template run overrides from metadata.
- Parameters:
metadata (dict) – The metadata from which to extract overrides.
- Returns:
A tuple containing the updated metadata and the extracted overrides.
- Return type:
tuple
- get_raw_template(template_name)[source]¶
Retrieve the raw source of a template by its name.
- Parameters:
template_name (str) – The name of the template to retrieve.
- Returns:
A tuple containing a boolean success flag, the raw template source as a string, and a user message.
- Return type:
tuple
- get_template_and_variables(template_name)[source]¶
Fetches a template and its variables.
- Parameters:
template_name (str) – The name of the template to fetch
- Returns:
The fetched template and its variables, or (None, None) if the template is not found
- Return type:
tuple
- get_template_editable_filepath(template_name)[source]¶
Get the editable file path for a given template.
- Parameters:
template_name (str) – The name of the template
- Returns:
A tuple containing a boolean indicating if the template is editable, the file path of the template, and a message
- Return type:
tuple
- get_template_source(template_name)[source]¶
Get the source of a specified template.
- Parameters:
template_name (str) – The name of the template
- Returns:
A tuple containing a boolean indicating success, the source of the template if successful, and a user message
- Return type:
tuple
- get_template_variables_substitutions(template_name)[source]¶
Get template variables and their substitutions.
- Parameters:
template_name (str) – The name of the template
- Returns:
A tuple containing a boolean indicating success, the template with its variables and substitutions, and a user message
- Return type:
tuple
- is_system_template(filepath)[source]¶
Check if a file is a system template.
- Parameters:
filepath (str) – The path of the file to check
- Returns:
True if the file is a system template, False otherwise
- Return type:
bool
- make_temp_template(template_contents, suffix='md')[source]¶
Create a temporary template.
- Parameters:
template_contents (str) – The contents to be written to the temporary template
suffix (str, optional) – The suffix for the temporary file, defaults to ‘md’
- Returns:
The basename and the full path of the temporary template
- Return type:
tuple
- make_temp_template_dir()[source]¶
Create directory for temporary templates if it does not exist.
- Returns:
None
- make_user_template_dirs()[source]¶
Create directories for user templates if they do not exist.
- Returns:
None
- process_template_builtin_variables(template_name, variables=None)[source]¶
Process the built-in variables in a template.
- Parameters:
template_name (str) – The name of the template
variables (list, optional) – The variables to be processed, defaults to None
- Returns:
A dictionary of substitutions for the variables
- Return type:
dict
- remove_temp_template(template_name)[source]¶
Remove a temporary template.
- Parameters:
template_name (str) – The name of the temporary template
- Returns:
None
- render_template(template_name)[source]¶
Render a template with variable substitutions.
- Parameters:
template_name (str) – The name of the template to render
- Returns:
A tuple containing a success flag, the rendered message or template name, and a user message
- Return type:
tuple
- template_builtin_variables()[source]¶
This method returns a dictionary of built-in variables.
- Returns:
A dictionary where the key is the variable name and the value is the function associated with it.
- Return type:
dict
lwe.core.token_manager module¶
- class lwe.core.token_manager.TokenManager(config, provider, model_name, tool_cache)[source]¶
Bases:
objectManage model tokens.
- default_get_num_tokens_from_messages(messages, encoding=None)[source]¶
Get number of tokens for a list of messages.
The default implementation uses tiktoken, which is the OpenAI implementation.
- Parameters:
messages (list) – List of messages
encoding (Encoding, optional) – Encoding to use, defaults to None to auto-detect
- Returns:
Number of tokens
- Return type:
int
- get_num_tokens_from_messages(messages, encoding=None)[source]¶
Get number of tokens for a list of messages.
If a provider does not have a get num_tokens_from_messages() method, default_get_num_tokens_from_messages() will be used.
- Parameters:
messages (list) – List of messages
encoding (Encoding, optional) – Encoding to use, defaults to None to auto-detect
- Returns:
Number of tokens
- Return type:
int
lwe.core.util module¶
- lwe.core.util.extract_preset_configuration_from_request_overrides(request_overrides, active_preset_name=None)[source]¶
Extracts preset configuration from the given request overrides.
- Parameters:
request_overrides – The request overrides from which to extract preset configuration.
- Returns:
A tuple containing a success indicator, preset/preset_overrides/activate_preset configuration, and a user message.
lwe.core.workflow_manager module¶
- class lwe.core.workflow_manager.WorkflowManager(config=None)[source]¶
Bases:
objectManage workflows.
- copy_workflow(old_name, new_name)[source]¶
Copies a workflow file to a new location.
- Parameters:
old_name (str) – The name of the existing workflow file.
new_name (str) – The name for the new workflow file.
- Returns:
A tuple containing a boolean indicating success or failure, the new file path, and a status message.
- Return type:
tuple