Packages

Multi-surface application runtime for Elixir. One TEA module renders to terminal, browser (LiveView), SSH, and MCP (agents). 30+ widgets, flexbox + CSS grid, AI agent runtime, distributed swarm with CRDTs, time-travel debugging, session recording, sandboxed REPL, and agentic commerce.

Current section

Files

Jump to
raxol lib raxol ai content_generation.ex
Raw

lib/raxol/ai/content_generation.ex

defmodule Raxol.AI.ContentGeneration do
@moduledoc """
Content generation capabilities for Raxol applications.
This module provides AI-powered content generation for various aspects of
terminal UIs, including:
* Text suggestions
* Command completions
* Help content generation
* Documentation generation
* Interactive tutorials
* Contextual hints
The generation is configurable and can be adapted to different contexts
within the application.
"""
alias Raxol.Core.UXRefinement
alias Raxol.AI.ServiceAdapter
@type generation_type :: :text | :command | :help | :docs | :tutorial | :hint
@type generation_options :: %{
max_length: integer(),
style: atom(),
tone: atom(),
context: map(),
model: atom()
}
@doc """
Generates content based on the specified type and prompt.
## Options
* `:max_length` - Maximum length of the generated content (default: 100)
* `:style` - Style of the generated content (default: :neutral)
* `:tone` - Tone of the generated content (default: :neutral)
* `:context` - Additional context for generation (default: %{})
* `:model` - AI model to use for generation (default: :default)
## Examples
iex> generate(:text, "Create a welcome message", max_length: 50)
{:ok, "Welcome to the terminal! How can I assist you today?'"}
"""
@spec generate(generation_type(), String.t(), keyword()) ::
{:ok, String.t()} | {:error, String.t()}
def generate(type, prompt, opts \\ []) do
options = opts |> Enum.into(%{}) |> normalize_options()
# Check if AI features are enabled
if UXRefinement.feature_enabled?(:ai_content_generation) do
do_generate(type, prompt, options)
else
{:error, "AI content generation is not enabled"}
end
end
@doc """
Generates text suggestions based on current input context.
## Examples
iex> suggest_text("print(", context: %{language: :python})
{:ok, ["print(\"Hello, World!\")", "print(value)", "print(f\"Value: {value}\")"]}
"""
@spec suggest_text(String.t(), keyword()) ::
{:ok, [String.t()]} | {:error, String.t()}
def suggest_text(input, opts \\ []) do
options = opts |> Enum.into(%{}) |> normalize_options()
if UXRefinement.feature_enabled?(:ai_content_generation) do
do_suggest_text(input, options)
else
{:error, "AI content generation is not enabled"}
end
end
@doc """
Generates a contextual help document based on application state.
## Examples
iex> generate_help(%{current_view: :editor, command_mode: true})
{:ok, %{title: "Editor Mode Commands", content: "..."}}
"""
@spec generate_help(map(), keyword()) :: {:ok, map()} | {:error, String.t()}
def generate_help(context, opts \\ []) do
options = opts |> Enum.into(%{}) |> normalize_options()
if UXRefinement.feature_enabled?(:ai_content_generation) do
do_generate_help(context, options)
else
{:error, "AI content generation is not enabled"}
end
end
@doc """
Generates an interactive tutorial for a specific feature.
## Examples
iex> generate_tutorial(:keyboard_shortcuts)
{:ok, %{steps: [...], interactive: true}}
"""
@spec generate_tutorial(atom(), keyword()) ::
{:ok, map()} | {:error, String.t()}
def generate_tutorial(feature, opts \\ []) do
options = opts |> Enum.into(%{}) |> normalize_options()
if UXRefinement.feature_enabled?(:ai_content_generation) do
do_generate_tutorial(feature, options)
else
{:error, "AI content generation is not enabled"}
end
end
# Private helpers
defp do_generate(:text, prompt, options) do
ServiceAdapter.generate_content(prompt, %{
max_tokens: options.max_length,
temperature: 0.7
})
end
defp do_generate(:command, prompt, options) do
# Generate command completions
command =
"command #{prompt}"
|> String.slice(0, options.max_length)
{:ok, command}
end
defp do_generate(:help, prompt, options) do
# Generate help content
help =
"Help for: #{prompt}"
|> String.slice(0, options.max_length)
{:ok, help}
end
defp do_generate(:docs, prompt, options) do
# Generate documentation
docs =
"Documentation for: #{prompt}"
|> String.slice(0, options.max_length)
{:ok, docs}
end
defp do_generate(:tutorial, prompt, options) do
# Generate tutorial content
tutorial =
"Tutorial for: #{prompt}"
|> String.slice(0, options.max_length)
{:ok, tutorial}
end
defp do_generate(:hint, prompt, options) do
# Generate contextual hint
hint =
"Hint: #{prompt}"
|> String.slice(0, options.max_length)
{:ok, hint}
end
defp do_generate(_, _, _) do
{:error, "Unsupported generation type"}
end
defp do_suggest_text(input, options) do
context = Map.get(options, :context, %{})
ServiceAdapter.generate_suggestions(input, context)
end
defp do_generate_help(context, _options) do
# Placeholder: Help generation logic not yet implemented. Integrate with AI service here in the future.
{:ok, "Help content for: #{context}"}
end
defp do_generate_tutorial(feature, _options) do
# Placeholder: Tutorial generation logic not yet implemented. Integrate with AI service here in the future.
{:ok, "Tutorial for: #{feature}"}
end
defp normalize_options(options) do
defaults = %{
max_length: 100,
style: :neutral,
tone: :neutral,
context: %{},
model: :default
}
Map.merge(defaults, options)
end
end