Chat Command

Simplified chat command implementation.

ALWAYS_LEGAL_MESSAGE = 'Always review AI-generated content prior to use.'

Always good to have legal message.

DEFAULT_CHAT_DESCRIPTION = 'Default Command Line Assistant Chat.'

Default chat description when none is given

DEFAULT_CHAT_NAME = 'default'

Default chat name when none is given

class InputSource(question, stdin, attachment, attachment_mimetype, terminal_output)[source]

Input source for the chat command.

Parameters:
  • question (str) – The question to ask.

  • stdin (str) – The input from stdin.

  • attachment (str) – The attachment to use.

  • attachment_mimetype (str) – The mimetype of the attachment.

  • terminal_output (str) – The output from the terminal.

attachment: str
attachment_mimetype: str
get_input_source()[source]

Determine and return the appropriate input source based on combination rules.

Return type:

str

Returns:

The input source.

Return type:

str

question: str
stdin: str
terminal_output: str
LEGAL_NOTICE = "This feature uses AI technology. Do not include any personal information or other sensitive information in your input. Interactions may be used to improve Red Hat's products or services."

Legal notice that we need to output once per user

MAX_QUESTION_SIZE: int = 32000

Max input size we want to allow to be submitted to the backend. This corresponds to 32KB (32000 bytes)

_compose_message_input(render, context, input_source)[source]

Compose the final message that will be sent to the API.

Return type:

Question

Parameters:
  • render (RenderUtils) – The render utility.

  • context (CommandContext) – The command context.

  • input_source (InputSource) – The input source.

Returns:

The composed message input.

Return type:

Question

_create_chat_session(dbus, user_id, name, description)[source]

Create a new chat session for a given conversation.

Return type:

str

Parameters:
  • dbus (DbusUtils) – The DbusUtils object.

  • user_id (str) – The user ID.

  • name (str) – The name of the chat.

  • description (str) – The description of the chat.

Returns:

The ID of the created chat session.

Return type:

str

_delete_all_chats(render, dbus, user_id)[source]

Delete all chats operation.

Return type:

int

Parameters:
  • render (RenderUtils) – The RenderUtils instance.

  • dbus (DbusUtils) – The DbusUtils instance.

  • user_id (str) – The user ID.

Returns:

The exit code.

Return type:

int

_delete_chat(render, dbus, user_id, chat_name)[source]

Delete a specific chat operation.

Return type:

int

Parameters:
  • render (RenderUtils) – The RenderUtils instance.

  • dbus (DbusUtils) – The DbusUtils instance.

  • user_id (str) – The user ID.

  • chat_name (str) – The chat name.

Returns:

The exit code.

Return type:

int

_display_response(renderer, response)[source]

Display message to the terminal.

Return type:

None

Parameters:
  • renderer (Renderer) – The renderer to use.

  • response (str) – The response to display.

_gather_input_sources(args)[source]

Gather input sources from command-line arguments.

Return type:

InputSource

Parameters:

args (Namespace) – The command-line arguments.

Returns:

The gathered input sources.

Return type:

InputSource

_get_response(dbus, message_input, user_id)[source]

Get the response from the chat session.

Return type:

str

Parameters:
  • dbus (DbusUtils) – The DbusUtils instance.

  • message_input (Question) – The message input.

  • user_id (str) – The user ID.

Returns:

The response message.

Return type:

str

_handle_legal_message()[source]

Handle legal message screen output

Return type:

bool

Returns:

True if the legal message was handled successfully, False otherwise.

Return type:

bool

_interactive_chat(render, dbus, context, args, user_id, name, description)[source]

Interactive chat operation.

Return type:

int

Parameters:
  • render (RenderUtils) – The RenderUtils instance.

  • dbus (DbusUtils) – The DbusUtils instance.

  • context (CommandContext) – The CommandContext instance.

  • args (Namespace) – The Namespace instance.

  • user_id (str) – The user ID.

  • name (str) – The chat name.

  • description (str) – The chat description.

Returns:

The exit code.

Return type:

int

_list_chats(render, dbus, user_id)[source]

List all chats operation.

Return type:

int

Parameters:
  • render (RenderUtils) – The RenderUtils instance.

  • dbus (DbusUtils) – The DbusUtils instance.

  • user_id (str) – The user ID.

Returns:

The exit code.

Return type:

int

_parse_attachment_file(attachment=None)[source]

Parse attachment file and read its contents.

Return type:

str

Parameters:

attachment (Optional[TextIOWrapper]) – The attachment file to parse.

Returns:

The contents of the attachment file.

Return type:

str

_read_last_terminal_output(index)[source]

Read the last terminal output by index.

Return type:

str

Parameters:

index (int) – The index of the terminal output to read.

Returns:

The contents of the terminal output.

Return type:

str

_single_question(render, dbus, context, args, user_id, name, description)[source]

Single question operation.

Return type:

int

Parameters:
  • render (RenderUtils) – The RenderUtils instance.

  • dbus (DbusUtils) – The DbusUtils instance.

  • context (CommandContext) – The CommandContext instance.

  • args (Namespace) – The Namespace instance.

  • user_id (str) – The user ID.

  • name (str) – The chat name.

  • description (str) – The chat description.

Returns:

The exit code.

Return type:

int

_submit_question(dbus, user_id, chat_id, message_input, plain)[source]

Submit the question over dbus.

Return type:

str

Parameters:
  • dbus (DbusUtils) – The dbus utils object.

  • user_id (str) – The user id.

  • chat_id (str) – The chat id.

  • message_input (Question) – The question.

  • plain (bool) – Whether to render in plain text.

Returns:

The response.

Return type:

str

_trim_message_size(render, question)[source]

Trim the message size to fit within the maximum allowed size.

Return type:

str

Parameters:
  • render (RenderUtils) – The render object to display warnings.

  • question (str) – The question to be trimmed.

Returns:

The trimmed question.

Return type:

str

_validate_query_composition(args)[source]

Valid if the query compostion is valid or not.

Return type:

Optional[str]

Parameters:

args (Namespace) – The Namespace instance.

Returns:

In case the query is not valid by any means in the validation.

Return type:

Optional[str]